diff options
author | John Stebbins <[email protected]> | 2018-06-13 10:11:30 -0700 |
---|---|---|
committer | John Stebbins <[email protected]> | 2018-06-13 10:11:30 -0700 |
commit | c85294a8f0bce69d5cb417f60a143663fb83772a (patch) | |
tree | e6f68e663722653d93b7069c8fc75a0425fd8d2c /libhb | |
parent | 256c9acab3e49d61bb3a48c8fe60c7b17c09bad6 (diff) |
libhb: fix snprintf compiler warnings
Our handling of temporary directory paths could truncate resulting
filenames. This fixes the warnings and prevents possible truncation.
Diffstat (limited to 'libhb')
-rw-r--r-- | libhb/encavcodec.c | 13 | ||||
-rw-r--r-- | libhb/enctheora.c | 6 | ||||
-rw-r--r-- | libhb/encx264.c | 6 | ||||
-rw-r--r-- | libhb/encx265.c | 18 | ||||
-rw-r--r-- | libhb/hb.c | 74 | ||||
-rw-r--r-- | libhb/ports.c | 41 | ||||
-rw-r--r-- | libhb/ports.h | 13 |
7 files changed, 100 insertions, 71 deletions
diff --git a/libhb/encavcodec.c b/libhb/encavcodec.c index 805365ac2..ee914faba 100644 --- a/libhb/encavcodec.c +++ b/libhb/encavcodec.c @@ -368,17 +368,18 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job ) if( job->pass_id == HB_PASS_ENCODE_1ST || job->pass_id == HB_PASS_ENCODE_2ND ) { - char filename[1024]; memset( filename, 0, 1024 ); - hb_get_tempory_filename( job->h, filename, "ffmpeg.log" ); + char * filename = hb_get_temporary_filename("ffmpeg.log"); if( job->pass_id == HB_PASS_ENCODE_1ST ) { pv->file = hb_fopen(filename, "wb"); - if (!pv->file) { + if (!pv->file) + { if (strerror_r(errno, reason, 79) != 0) strcpy(reason, "unknown -- strerror_r() failed"); hb_error("encavcodecInit: Failed to open %s (reason: %s)", filename, reason); + free(filename); ret = 1; goto done; } @@ -395,6 +396,7 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job ) strcpy(reason, "unknown -- strerror_r() failed"); hb_error("encavcodecInit: Failed to open %s (reason: %s)", filename, reason); + free(filename); ret = 1; goto done; } @@ -412,6 +414,7 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job ) strcpy(reason, "unknown -- strerror_r() failed"); hb_error( "encavcodecInit: Failed to read %s (reason: %s)" , filename, reason); + free(filename); ret = 1; fclose( pv->file ); pv->file = NULL; @@ -424,12 +427,14 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job ) context->flags |= AV_CODEC_FLAG_PASS2; context->stats_in = log; } + free(filename); } if (hb_avcodec_open(context, codec, &av_opts, HB_FFMPEG_THREADS_AUTO)) { hb_log( "encavcodecInit: avcodec_open failed" ); - return 1; + ret = 1; + goto done; } if (job->pass_id == HB_PASS_ENCODE_1ST && diff --git a/libhb/enctheora.c b/libhb/enctheora.c index 3a78d7228..85c295b00 100644 --- a/libhb/enctheora.c +++ b/libhb/enctheora.c @@ -47,9 +47,8 @@ int enctheoraInit( hb_work_object_t * w, hb_job_t * job ) if( job->pass_id == HB_PASS_ENCODE_1ST || job->pass_id == HB_PASS_ENCODE_2ND ) { - char filename[1024]; - memset( filename, 0, 1024 ); - hb_get_tempory_filename( job->h, filename, "theroa.log" ); + char * filename; + filename = hb_get_temporary_filename("theroa.log"); if ( job->pass_id == HB_PASS_ENCODE_1ST ) { pv->file = hb_fopen(filename, "wb"); @@ -58,6 +57,7 @@ int enctheoraInit( hb_work_object_t * w, hb_job_t * job ) { pv->file = hb_fopen(filename, "rb"); } + free(filename); } th_info ti; diff --git a/libhb/encx264.c b/libhb/encx264.c index 638cf4ff7..6df1f9109 100644 --- a/libhb/encx264.c +++ b/libhb/encx264.c @@ -38,7 +38,7 @@ struct hb_work_private_s hb_chapter_queue_t * chapter_queue; - char filename[1024]; + char * filename; // Multiple bit-depth const x264_api_t * api; @@ -520,8 +520,7 @@ int encx264Init( hb_work_object_t * w, hb_job_t * job ) if( job->pass_id == HB_PASS_ENCODE_1ST || job->pass_id == HB_PASS_ENCODE_2ND ) { - memset( pv->filename, 0, 1024 ); - hb_get_tempory_filename( job->h, pv->filename, "x264.log" ); + pv->filename = hb_get_temporary_filename("x264.log"); } switch( job->pass_id ) { @@ -637,6 +636,7 @@ void encx264Close( hb_work_object_t * w ) hb_chapter_queue_close(&pv->chapter_queue); pv->api->encoder_close( pv->x264 ); + free( pv->filename ); free( pv ); w->private_data = NULL; } diff --git a/libhb/encx265.c b/libhb/encx265.c index 1f66228fd..cce69e330 100644 --- a/libhb/encx265.c +++ b/libhb/encx265.c @@ -53,7 +53,7 @@ struct hb_work_private_s int64_t duration; } frame_info[FRAME_INFO_SIZE]; - char csvfn[1024]; + char * csvfn; // Multiple bit-depth const x265_api * api; @@ -255,15 +255,17 @@ int encx265Init(hb_work_object_t *w, hb_job_t *job) if (job->pass_id == HB_PASS_ENCODE_1ST || job->pass_id == HB_PASS_ENCODE_2ND) { - char stats_file[1024] = ""; - char pass[2]; + char * stats_file; + char pass[2]; snprintf(pass, sizeof(pass), "%d", job->pass_id); - hb_get_tempory_filename(job->h, stats_file, "x265.log"); + stats_file = hb_get_temporary_filename("x265.log"); if (param_parse(pv, param, "stats", stats_file) || param_parse(pv, param, "pass", pass)) { + free(stats_file); goto fail; } + free(stats_file); if (job->pass_id == HB_PASS_ENCODE_1ST) { char slowfirstpass[2]; @@ -278,17 +280,16 @@ int encx265Init(hb_work_object_t *w, hb_job_t *job) } /* statsfile (but not 2-pass) */ - memset(pv->csvfn, 0, sizeof(pv->csvfn)); if (param->logLevel >= X265_LOG_DEBUG) { if (param->csvfn == NULL) { - hb_get_tempory_filename(job->h, pv->csvfn, "x265.csv"); - param->csvfn = pv->csvfn; + pv->csvfn = hb_get_temporary_filename("x265.csv"); + param->csvfn = strdup(pv->csvfn); } else { - strncpy(pv->csvfn, param->csvfn, sizeof(pv->csvfn)); + pv->csvfn = strdup(param->csvfn); } } @@ -354,6 +355,7 @@ void encx265Close(hb_work_object_t *w) pv->api->param_free(pv->param); pv->api->encoder_close(pv->x265); + free(pv->csvfn); free(pv); w->private_data = NULL; } diff --git a/libhb/hb.c b/libhb/hb.c index c63e0e069..e4b0e1110 100644 --- a/libhb/hb.c +++ b/libhb/hb.c @@ -308,17 +308,20 @@ int hb_get_build( hb_handle_t * h ) */ void hb_remove_previews( hb_handle_t * h ) { - char filename[1024]; - char dirname[1024]; + char * filename; + char * dirname; hb_title_t * title; int i, count, len; DIR * dir; struct dirent * entry; - memset( dirname, 0, 1024 ); - hb_get_temporary_directory( dirname ); + dirname = hb_get_temporary_directory(); dir = opendir( dirname ); - if (dir == NULL) return; + if (dir == NULL) + { + free(dirname); + return; + } count = hb_list_count( h->title_set.list_title ); while( ( entry = readdir( dir ) ) ) @@ -330,15 +333,20 @@ void hb_remove_previews( hb_handle_t * h ) for( i = 0; i < count; i++ ) { title = hb_list_item( h->title_set.list_title, i ); + filename = hb_strdup_printf("%d_%d", h->id, title->index); len = snprintf( filename, 1024, "%d_%d", h->id, title->index ); if (strncmp(entry->d_name, filename, len) == 0) { - snprintf( filename, 1024, "%s/%s", dirname, entry->d_name ); + free(filename); + filename = hb_strdup_printf("%s/%s", dirname, entry->d_name); unlink( filename ); + free(filename); break; } + free(filename); } } + free(dirname); closedir( dir ); } @@ -444,19 +452,21 @@ hb_title_set_t * hb_get_title_set( hb_handle_t * h ) int hb_save_preview( hb_handle_t * h, int title, int preview, hb_buffer_t *buf ) { FILE * file; - char filename[1024]; + char * filename; char reason[80]; - hb_get_tempory_filename( h, filename, "%d_%d_%d", - hb_get_instance_id(h), title, preview ); + filename = hb_get_temporary_filename("%d_%d_%d", hb_get_instance_id(h), + title, preview ); file = hb_fopen(filename, "wb"); - if( !file ) + if (file == NULL) { if (strerror_r(errno, reason, 79) != 0) strcpy(reason, "unknown -- strerror_r() failed"); - hb_error( "hb_save_preview: Failed to open %s (reason: %s)", filename, reason ); + hb_error("hb_save_preview: Failed to open %s (reason: %s)", + filename, reason); + free(filename); return -1; } @@ -477,7 +487,8 @@ int hb_save_preview( hb_handle_t * h, int title, int preview, hb_buffer_t *buf ) if (strerror_r(errno, reason, 79) != 0) strcpy(reason, "unknown -- strerror_r() failed"); - hb_error( "hb_save_preview: Failed to write line %d to %s (reason: %s). Preview will be incomplete.", + hb_error( "hb_save_preview: Failed to write line %d to %s " + "(reason: %s). Preview will be incomplete.", hh, filename, reason ); goto done; } @@ -487,6 +498,7 @@ int hb_save_preview( hb_handle_t * h, int title, int preview, hb_buffer_t *buf ) } done: + free(filename); fclose( file ); return 0; @@ -495,19 +507,21 @@ done: hb_buffer_t * hb_read_preview(hb_handle_t * h, hb_title_t *title, int preview) { FILE * file; - char filename[1024]; + char * filename; char reason[80]; - hb_get_tempory_filename(h, filename, "%d_%d_%d", - hb_get_instance_id(h), title->index, preview); + filename = hb_get_temporary_filename("%d_%d_%d", hb_get_instance_id(h), + title->index, preview); file = hb_fopen(filename, "rb"); - if (!file) + if (file == NULL) { if (strerror_r(errno, reason, 79) != 0) strcpy(reason, "unknown -- strerror_r() failed"); - hb_error( "hb_read_preview: Failed to open %s (reason: %s)", filename, reason ); + hb_error("hb_read_preview: Failed to open %s (reason: %s)", + filename, reason); + free(filename); return NULL; } @@ -535,8 +549,9 @@ hb_buffer_t * hb_read_preview(hb_handle_t * h, hb_title_t *title, int preview) if (strerror_r(errno, reason, 79) != 0) strcpy(reason, "unknown -- strerror_r() failed"); - hb_error( "hb_read_preview: Failed to read line %d from %s (reason: %s). Preview will be incomplete.", - hh, filename, reason ); + hb_error("hb_read_preview: Failed to read line %d from %s " + "(reason: %s). Preview will be incomplete.", + hh, filename, reason ); goto done; } } @@ -545,6 +560,7 @@ hb_buffer_t * hb_read_preview(hb_handle_t * h, hb_title_t *title, int preview) } done: + free(filename); fclose(file); return buf; @@ -1703,33 +1719,33 @@ int hb_global_init() */ void hb_global_close() { - char dirname[1024]; - DIR * dir; + char * dirname; + DIR * dir; struct dirent * entry; hb_presets_free(); /* Find and remove temp folder */ - memset( dirname, 0, 1024 ); - hb_get_temporary_directory( dirname ); + dirname = hb_get_temporary_directory(); dir = opendir( dirname ); if (dir) { while( ( entry = readdir( dir ) ) ) { - char filename[1024]; + char * filename; if( entry->d_name[0] == '.' ) { continue; } - memset( filename, 0, 1024 ); - snprintf( filename, 1023, "%s/%s", dirname, entry->d_name ); + filename = hb_strdup_printf("%s/%s", dirname, entry->d_name); unlink( filename ); + free(filename); } closedir( dir ); rmdir( dirname ); } + free(dirname); } /** @@ -1741,15 +1757,15 @@ void hb_global_close() static void thread_func( void * _h ) { hb_handle_t * h = (hb_handle_t *) _h; - char dirname[1024]; + char * dirname; h->pid = getpid(); /* Create folder for temporary files */ - memset( dirname, 0, 1024 ); - hb_get_temporary_directory( dirname ); + dirname = hb_get_temporary_directory(); hb_mkdir( dirname ); + free(dirname); while( !h->die ) { diff --git a/libhb/ports.c b/libhb/ports.c index 5d71541ab..ac08c368a 100644 --- a/libhb/ports.c +++ b/libhb/ports.c @@ -598,17 +598,17 @@ void hb_get_user_config_filename( char name[1024], char *fmt, ... ) /************************************************************************ * Get a temporary directory for HB ***********************************************************************/ -void hb_get_temporary_directory( char path[512] ) +char * hb_get_temporary_directory() { - char base[512]; - char *p; + char * path, * base, * p; /* Create the base */ #if defined( SYS_CYGWIN ) || defined( SYS_MINGW ) - int i_size = GetTempPath( 512, base ); - if( i_size <= 0 || i_size >= 512 ) + base = malloc(MAX_PATH); + int i_size = GetTempPath( MAX_PATH, base ); + if( i_size <= 0 || i_size >= MAX_PATH ) { - if( getcwd( base, 512 ) == NULL ) + if( getcwd( base, MAX_PATH ) == NULL ) strcpy( base, "c:" ); /* Bad fallback but ... */ } @@ -617,32 +617,39 @@ void hb_get_temporary_directory( char path[512] ) *p = '/'; #else if( (p = getenv( "TMPDIR" ) ) != NULL || - (p = getenv( "TEMP" ) ) != NULL ) - strcpy( base, p ); + (p = getenv( "TEMP" ) ) != NULL ) + base = strdup(p); else - strcpy( base, "/tmp" ); + base = strdup("/tmp"); #endif /* I prefer to remove evntual last '/' (for cygwin) */ if( base[strlen(base)-1] == '/' ) base[strlen(base)-1] = '\0'; - snprintf(path, 512, "%s/hb.%d", base, (int)getpid()); + path = hb_strdup_printf("%s/hb.%d", base, (int)getpid()); + free(base); + + return path; } /************************************************************************ * Get a tempory filename for HB ***********************************************************************/ -void hb_get_tempory_filename( hb_handle_t * h, char name[1024], - char *fmt, ... ) +char * hb_get_temporary_filename( char *fmt, ... ) { - va_list args; - - hb_get_temporary_directory( name ); - strcat( name, "/" ); + va_list args; + char * name, * path; + char * dir = hb_get_temporary_directory(); va_start( args, fmt ); - vsnprintf( &name[strlen(name)], 1024 - strlen(name), fmt, args ); + name = hb_strdup_vaprintf(fmt, args); va_end( args ); + + path = hb_strdup_printf("%s/%s", dir, name); + free(dir); + free(name); + + return path; } /************************************************************************ diff --git a/libhb/ports.h b/libhb/ports.h index f664170a4..6e78bea10 100644 --- a/libhb/ports.h +++ b/libhb/ports.h @@ -83,6 +83,12 @@ int hb_stat(const char *path, hb_stat_t *sb); FILE * hb_fopen(const char *path, const char *mode); char * hb_strr_dir_sep(const char *path); +/************************************************************************ + * File utils + ***********************************************************************/ +char * hb_get_temporary_directory(void); +char * hb_get_temporary_filename( char *fmt, ... ); + #ifdef __LIBHB__ // Convert utf8 string to current code page. @@ -95,13 +101,6 @@ char * hb_utf8_to_cp(const char *src); ***********************************************************************/ int hb_dvd_region(char *device, int *region_mask); -/************************************************************************ - * File utils - ***********************************************************************/ -void hb_get_temporary_directory( char path[512] ); -void hb_get_tempory_filename( hb_handle_t *, char name[1024], - char * fmt, ... ); - #if defined( SYS_DARWIN ) int osx_get_user_config_directory( char path[512] ); #endif |