@@ -521,7 +521,11 @@ static void event_image_detect(struct context *cnt,
521521 imagepath = DEF_IMAGEPATH ;
522522
523523 mystrftime (cnt , filename , sizeof (filename ), imagepath , currenttime_tv , NULL , 0 );
524- snprintf (fullfilename , PATH_MAX , "%s/%s.%s" , cnt -> conf .target_dir , filename , imageext (cnt ));
524+ snprintf (fullfilename , PATH_MAX , "%.*s/%.*s.%s"
525+ , (int )(PATH_MAX - 2 - strlen (filename )- strlen (imageext (cnt )))
526+ , cnt -> conf .target_dir
527+ , (int )(PATH_MAX - 2 - strlen (cnt -> conf .target_dir )- strlen (imageext (cnt )))
528+ , filename , imageext (cnt ));
525529
526530 passthrough = util_check_passthrough (cnt );
527531 if ((cnt -> imgs .size_high > 0 ) && (!passthrough )) {
@@ -558,9 +562,14 @@ static void event_imagem_detect(struct context *cnt,
558562 mystrftime (cnt , filename , sizeof (filename ), imagepath , currenttime_tv , NULL , 0 );
559563
560564 /* motion images gets same name as normal images plus an appended 'm' */
561- snprintf (filenamem , PATH_MAX , "%sm" , filename );
562- snprintf (fullfilenamem , PATH_MAX , "%s/%s.%s" , cnt -> conf .target_dir , filenamem , imageext (cnt ));
563-
565+ snprintf (filenamem , PATH_MAX , "%.*sm"
566+ , (int )(PATH_MAX - 1 - strlen (filename ))
567+ , filename );
568+ snprintf (fullfilenamem , PATH_MAX , "%.*s/%.*s.%s"
569+ , (int )(PATH_MAX - 2 - strlen (filenamem )- strlen (imageext (cnt )))
570+ , cnt -> conf .target_dir
571+ , (int )(PATH_MAX - 2 - strlen (cnt -> conf .target_dir )- strlen (imageext (cnt )))
572+ , filenamem , imageext (cnt ));
564573 put_picture (cnt , fullfilenamem , cnt -> imgs .img_motion .image_norm , FTYPE_IMAGE_MOTION );
565574 event (cnt , EVENT_FILECREATE , NULL , fullfilenamem , (void * )FTYPE_IMAGE , currenttime_tv );
566575 }
@@ -593,16 +602,25 @@ static void event_image_snapshot(struct context *cnt,
593602 snappath = DEF_SNAPPATH ;
594603
595604 mystrftime (cnt , filepath , sizeof (filepath ), snappath , currenttime_tv , NULL , 0 );
596- snprintf (filename , PATH_MAX , "%s.%s" , filepath , imageext (cnt ));
597- snprintf (fullfilename , PATH_MAX , "%s/%s" , cnt -> conf .target_dir , filename );
605+ snprintf (filename , PATH_MAX , "%.*s.%s"
606+ , (int )(PATH_MAX - 1 - strlen (filepath )- strlen (imageext (cnt )))
607+ , filepath , imageext (cnt ));
608+ snprintf (fullfilename , PATH_MAX , "%.*s/%.*s"
609+ , (int )(PATH_MAX - 1 - strlen (filename ))
610+ , cnt -> conf .target_dir
611+ , (int )(PATH_MAX - 1 - strlen (cnt -> conf .target_dir ))
612+ , filename );
598613 put_picture (cnt , fullfilename , img_data -> image_norm , FTYPE_IMAGE_SNAPSHOT );
599614 event (cnt , EVENT_FILECREATE , NULL , fullfilename , (void * )FTYPE_IMAGE , currenttime_tv );
600615
601616 /*
602617 * Update symbolic link *after* image has been written so that
603618 * the link always points to a valid file.
604619 */
605- snprintf (linkpath , PATH_MAX , "%s/lastsnap.%s" , cnt -> conf .target_dir , imageext (cnt ));
620+ snprintf (linkpath , PATH_MAX , "%.*s/lastsnap.%s"
621+ , (int )(PATH_MAX - strlen ("/lastsnap." )- strlen (imageext (cnt )))
622+ , cnt -> conf .target_dir , imageext (cnt ));
623+
606624 remove (linkpath );
607625
608626 if (symlink (filename , linkpath )) {
@@ -612,8 +630,14 @@ static void event_image_snapshot(struct context *cnt,
612630 }
613631 } else {
614632 mystrftime (cnt , filepath , sizeof (filepath ), cnt -> conf .snapshot_filename , currenttime_tv , NULL , 0 );
615- snprintf (filename , PATH_MAX , "%s.%s" , filepath , imageext (cnt ));
616- snprintf (fullfilename , PATH_MAX , "%s/%s" , cnt -> conf .target_dir , filename );
633+ snprintf (filename , PATH_MAX , "%.*s.%s"
634+ , (int )(PATH_MAX - 1 - strlen (imageext (cnt )))
635+ , filepath , imageext (cnt ));
636+ snprintf (fullfilename , PATH_MAX , "%.*s/%.*s"
637+ , (int )(PATH_MAX - 1 - strlen (filename ))
638+ , cnt -> conf .target_dir
639+ , (int )(PATH_MAX - 1 - strlen (cnt -> conf .target_dir ))
640+ , filename );
617641 remove (fullfilename );
618642 put_picture (cnt , fullfilename , img_data -> image_norm , FTYPE_IMAGE_SNAPSHOT );
619643 event (cnt , EVENT_FILECREATE , NULL , fullfilename , (void * )FTYPE_IMAGE , currenttime_tv );
@@ -687,7 +711,11 @@ static void event_image_preview(struct context *cnt,
687711 imagepath = (char * )DEF_IMAGEPATH ;
688712
689713 mystrftime (cnt , filename , sizeof (filename ), imagepath , & cnt -> imgs .preview_image .timestamp_tv , NULL , 0 );
690- snprintf (previewname , PATH_MAX , "%s/%s.%s" , cnt -> conf .target_dir , filename , imageext (cnt ));
714+ snprintf (previewname , PATH_MAX , "%.*s/%.*s.%s"
715+ , (int )(PATH_MAX - 2 - strlen (filename )- strlen (imageext (cnt )))
716+ , cnt -> conf .target_dir
717+ , (int )(PATH_MAX - 2 - strlen (cnt -> conf .target_dir )- strlen (imageext (cnt )))
718+ , filename , imageext (cnt ));
691719
692720 passthrough = util_check_passthrough (cnt );
693721 if ((cnt -> imgs .size_high > 0 ) && (!passthrough )) {
@@ -771,7 +799,11 @@ static void event_create_extpipe(struct context *cnt,
771799 }
772800
773801 mystrftime (cnt , stamp , sizeof (stamp ), moviepath , currenttime_tv , NULL , 0 );
774- snprintf (cnt -> extpipefilename , PATH_MAX - 4 , "%s/%s" , cnt -> conf .target_dir , stamp );
802+ snprintf (cnt -> extpipefilename , PATH_MAX - 4 , "%.*s/%.*s"
803+ , (int )(PATH_MAX - 5 - strlen (stamp ))
804+ , cnt -> conf .target_dir
805+ , (int )(PATH_MAX - 5 - strlen (cnt -> conf .target_dir ))
806+ , stamp );
775807
776808 if (access (cnt -> conf .target_dir , W_OK )!= 0 ) {
777809 /* Permission denied */
@@ -943,11 +975,27 @@ static void event_ffmpeg_newfile(struct context *cnt,
943975 codec = "msmpeg4" ;
944976 break ;
945977 }
946- snprintf (cnt -> motionfilename , PATH_MAX - 4 , "%s/%s_%sm" , cnt -> conf .target_dir , codec , stamp );
947- snprintf (cnt -> newfilename , PATH_MAX - 4 , "%s/%s_%s" , cnt -> conf .target_dir , codec , stamp );
978+ snprintf (cnt -> motionfilename , PATH_MAX - 4 , "%.*s/%s_%.*sm"
979+ , (int )(PATH_MAX - 7 - strlen (stamp )- strlen (codec ))
980+ , cnt -> conf .target_dir , codec
981+ , (int )(PATH_MAX - 7 - strlen (cnt -> conf .target_dir )- strlen (codec ))
982+ , stamp );
983+ snprintf (cnt -> newfilename , PATH_MAX - 4 , "%.*s/%s_%.*s"
984+ , (int )(PATH_MAX - 6 - strlen (stamp )- strlen (codec ))
985+ , cnt -> conf .target_dir , codec
986+ , (int )(PATH_MAX - 6 - strlen (cnt -> conf .target_dir )- strlen (codec ))
987+ , stamp );
948988 } else {
949- snprintf (cnt -> motionfilename , PATH_MAX - 4 , "%s/%sm" , cnt -> conf .target_dir , stamp );
950- snprintf (cnt -> newfilename , PATH_MAX - 4 , "%s/%s" , cnt -> conf .target_dir , stamp );
989+ snprintf (cnt -> motionfilename , PATH_MAX - 4 , "%.*s/%.*sm"
990+ , (int )(PATH_MAX - 6 - strlen (stamp ))
991+ , cnt -> conf .target_dir
992+ , (int )(PATH_MAX - 6 - strlen (cnt -> conf .target_dir ))
993+ , stamp );
994+ snprintf (cnt -> newfilename , PATH_MAX - 4 , "%.*s/%.*s"
995+ , (int )(PATH_MAX - 5 - strlen (stamp ))
996+ , cnt -> conf .target_dir
997+ , (int )(PATH_MAX - 5 - strlen (cnt -> conf .target_dir ))
998+ , stamp );
951999 }
9521000 if (cnt -> conf .movie_output ) {
9531001 cnt -> ffmpeg_output = mymalloc (sizeof (struct ffmpeg ));
@@ -1056,7 +1104,11 @@ static void event_ffmpeg_timelapse(struct context *cnt,
10561104 mystrftime (cnt , tmp , sizeof (tmp ), timepath , currenttime_tv , NULL , 0 );
10571105
10581106 /* PATH_MAX - 4 to allow for .mpg to be appended without overflow */
1059- snprintf (cnt -> timelapsefilename , PATH_MAX - 4 , "%s/%s" , cnt -> conf .target_dir , tmp );
1107+ snprintf (cnt -> timelapsefilename , PATH_MAX - 4 , "%.*s/%.*s"
1108+ , (int )(PATH_MAX - 5 - strlen (tmp ))
1109+ , cnt -> conf .target_dir
1110+ , (int )(PATH_MAX - 5 - strlen (cnt -> conf .target_dir ))
1111+ , tmp );
10601112 passthrough = util_check_passthrough (cnt );
10611113 cnt -> ffmpeg_timelapse = mymalloc (sizeof (struct ffmpeg ));
10621114 if ((cnt -> imgs .size_high > 0 ) && (!passthrough )){
0 commit comments