@@ -1068,35 +1068,48 @@ static char *GuessSubDir(char *channeldir, char *filename) {
10681068
10691069 localtime_r (& t , & t_tm );
10701070
1071- unsigned i = 0 ;
10721071 // if the file exists, it must be in any of the possible subdirs
10731072 // so try one after the next - one will match
1074- while ( subdir_def [i ]) {
1073+ for ( unsigned i = 0 ; subdir_def [i ]; i ++ ) {
10751074 char const * sub_fmt = subdir_def [i ];
10761075 char subpath [255 ];
10771076 struct stat stat_buf ;
1078- strftime (subpath , 254 , sub_fmt , & t_tm );
1079- subpath [254 ] = '\0' ;
1077+
1078+ strftime (subpath , sizeof (subpath ), sub_fmt , & t_tm );
1079+ subpath [sizeof (subpath ) - 1 ] = '\0' ;
10801080
10811081 if (prefix_len ) {
1082+ int pre = prefix_len > INT_MAX ? INT_MAX : (int )prefix_len ;
1083+
10821084 // Probe: <channeldir>/<subpath>/<prefix>/<base>
10831085 // Example: live/profileA/<guessed>/<2026/01/13>/<nfcapd...>
1084- snprintf (s , MAXPATHLEN - 1 , "%s/%s/%.*s/%s" , channeldir , subpath , (int )prefix_len , filename , base );
1086+ int n = snprintf (s , sizeof (s ), "%s/%s/%.*s/%s" , channeldir , subpath , pre , filename , base );
1087+ s [sizeof (s ) - 1 ] = '\0' ;
1088+ if (n > 0 && (size_t )n < sizeof (s ) && stat (s , & stat_buf ) == 0 && S_ISREG (stat_buf .st_mode )) {
1089+ dbg_printf ("GuessSubDir() found: %s\n" , subpath );
1090+ return strdup (subpath );
1091+ }
1092+
1093+ // Optional but recommended:
1094+ // Also probe without the prefix, in case the provided prefix is not part of the on-disk hierarchy
1095+ n = snprintf (s , sizeof (s ), "%s/%s/%s" , channeldir , subpath , base );
1096+ s [sizeof (s ) - 1 ] = '\0' ;
1097+ if (n > 0 && (size_t )n < sizeof (s ) && stat (s , & stat_buf ) == 0 && S_ISREG (stat_buf .st_mode )) {
1098+ dbg_printf ("GuessSubDir() found: %s\n" , subpath );
1099+ return strdup (subpath );
1100+ }
10851101 } else {
10861102 // Probe: <channeldir>/<subpath>/<base>
1087- snprintf (s , MAXPATHLEN - 1 , "%s/%s/%s" , channeldir , subpath , base );
1088- }
1089-
1090- if (stat (s , & stat_buf ) == 0 && S_ISREG (stat_buf .st_mode )) {
1091- // found file in subdir
1092- dbg_printf ("GuessSubDir() found: %s\n" , subpath );
1093- return strdup (subpath );
1103+ int n = snprintf (s , sizeof (s ), "%s/%s/%s" , channeldir , subpath , base );
1104+ s [sizeof (s ) - 1 ] = '\0' ;
1105+ if (n > 0 && (size_t )n < sizeof (s ) && stat (s , & stat_buf ) == 0 && S_ISREG (stat_buf .st_mode )) {
1106+ dbg_printf ("GuessSubDir() found: %s\n" , subpath );
1107+ return strdup (subpath );
1108+ }
10941109 }
1095- i ++ ;
10961110 }
10971111
10981112 return NULL ;
1099-
11001113} // End of GuessSubDir
11011114
11021115// make sure, that path with subdir exists for timeslot now and stores it into path
0 commit comments