From eb4475fa7b3b00f9c1ee3f3ac2085a080b6b12f9 Mon Sep 17 00:00:00 2001 From: "minje.ahn" Date: Wed, 17 Aug 2022 15:53:03 +0900 Subject: [PATCH] Fix coverity issue 1. Fixed PW.BRANCH_PAST_INITIALIZATION in mm_file_format_ffmpeg.c 2. Fixed CHECKED_RETURN related to g_strlcpy Change-Id: Ibcdf56eb91b89de8409061a5677abfa8e3bedf72 Signed-off-by: minje.ahn --- formats/ffmpeg/mm_file_format_ffmpeg.c | 45 +++++-------- include/mm_file_debug.h | 4 -- tests/mm_file_test.c | 113 ++++++++++++--------------------- utils/mm_file_util_io.c | 12 ++-- utils/mm_file_util_tag.c | 8 ++- 5 files changed, 69 insertions(+), 113 deletions(-) diff --git a/formats/ffmpeg/mm_file_format_ffmpeg.c b/formats/ffmpeg/mm_file_format_ffmpeg.c index 3e2cff6..9c6d259 100644 --- a/formats/ffmpeg/mm_file_format_ffmpeg.c +++ b/formats/ffmpeg/mm_file_format_ffmpeg.c @@ -783,11 +783,12 @@ int mmfile_format_read_tag_ffmpg(MMFileFormatContext *formatContext) int mmfile_format_read_frame_ffmpg(MMFileFormatContext *formatContext, unsigned int timestamp, MMFileFormatFrame *frame) { - AVFormatContext *pFormatCtx = NULL; - AVCodecContext *pVideoCodecCtx = NULL; - AVCodec *pVideoCodec = NULL; + AVFormatContext *pFormatCtx = NULL; + AVCodecContext *pVideoCodecCtx = NULL; + AVCodec *pVideoCodec = NULL; AVCodecParameters *pVideoCodecPar = NULL; - AVFrame *pFrame = NULL; + AVFrame *pFrame = NULL; + struct SwsContext *img_convert_ctx = NULL; int width = 0; int height = 0; @@ -848,8 +849,7 @@ int mmfile_format_read_frame_ffmpg(MMFileFormatContext *formatContext, unsigned ret = _get_first_good_video_frame(pFormatCtx, pVideoCodecCtx, &pFrame, formatContext->cdis); if (ret != MMFILE_FORMAT_SUCCESS) { debug_error(DEBUG, "error: get key frame"); - ret = MMFILE_FORMAT_FAIL; - goto exception; + goto EXCEPTION; } debug_msg(RELEASE, "Video default resolution = [%dx%d]", pVideoCodecCtx->coded_width, pVideoCodecCtx->coded_height); @@ -866,15 +866,13 @@ int mmfile_format_read_frame_ffmpg(MMFileFormatContext *formatContext, unsigned numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, width, height, 1); if (numBytes <= 0) { debug_error(DEBUG, "error: av_image_get_buffer_size. [%d x %d]", width, height); - ret = MMFILE_FORMAT_FAIL; - goto exception; + goto EXCEPTION; } frame->frameData = av_malloc(numBytes); if (!frame->frameData) { debug_error(DEBUG, "error: av_malloc."); - ret = MMFILE_FORMAT_FAIL; - goto exception; + goto EXCEPTION; } uint8_t *dst_data[4]; @@ -882,28 +880,23 @@ int mmfile_format_read_frame_ffmpg(MMFileFormatContext *formatContext, unsigned ret = av_image_fill_arrays(dst_data, dst_linesize, frame->frameData, AV_PIX_FMT_RGB24, width, height, 1); if (ret < 0) { - debug_error(DEBUG, "error: avpicture_fill fail. errcode = 0x%08X", ret); - ret = MMFILE_FORMAT_FAIL; - goto exception; + debug_error(DEBUG, "error: av_image_fill_arrays fail. errcode = 0x%08X", ret); + goto EXCEPTION; } - struct SwsContext *img_convert_ctx = NULL; - img_convert_ctx = sws_getContext(width, height, pVideoCodecCtx->pix_fmt, width, height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); if (NULL == img_convert_ctx) { - debug_error(DEBUG, "failed to get img convet ctx"); - ret = MMFILE_FORMAT_FAIL; - goto exception; + debug_error(DEBUG, "failed to get img convert ctx"); + goto EXCEPTION; } ret = sws_scale(img_convert_ctx, (const uint8_t * const *)pFrame->data, pFrame->linesize, 0, height, dst_data, dst_linesize); if (ret < 0) { - debug_error(DEBUG, "failed to convet image"); - ret = MMFILE_FORMAT_FAIL; + debug_error(DEBUG, "failed to convert image"); sws_freeContext(img_convert_ctx); img_convert_ctx = NULL; - goto exception; + goto EXCEPTION; } sws_freeContext(img_convert_ctx); @@ -922,14 +915,10 @@ int mmfile_format_read_frame_ffmpg(MMFileFormatContext *formatContext, unsigned return MMFILE_FORMAT_SUCCESS; -exception: - if (pVideoCodecCtx) - avcodec_free_context(&pVideoCodecCtx); - +EXCEPTION: + avcodec_free_context(&pVideoCodecCtx); av_freep(&frame->frameData); - - if (pFrame) - av_frame_free(&pFrame); + av_frame_free(&pFrame); return MMFILE_FORMAT_FAIL; } diff --git a/include/mm_file_debug.h b/include/mm_file_debug.h index 75e9f94..5b2c1f0 100755 --- a/include/mm_file_debug.h +++ b/include/mm_file_debug.h @@ -42,9 +42,6 @@ extern "C" { #define DEBUG_MODE 0 #endif -#define SAFE_STRLCPY(dst, src, n) g_strlcpy(dst, src, n); -#define SAFE_STRLCAT(dst, src, n) g_strlcat(dst, src, n); - /*#define LOG_COLOR */ #ifdef LOG_COLOR @@ -186,4 +183,3 @@ extern "C" { #endif #endif /* _MMFILE_DEBUG_H_ */ - diff --git a/tests/mm_file_test.c b/tests/mm_file_test.c index a1938ab..f808383 100755 --- a/tests/mm_file_test.c +++ b/tests/mm_file_test.c @@ -25,7 +25,6 @@ #include #include -#include #include #include @@ -177,8 +176,8 @@ const char *VideoCodecTypeString[] = { static FILE *fpFailList = NULL; -static int mmfile_get_file_infomation(void *data, void *user_data, bool file_test); -static int mmfile_get_video_frame(void *data, void *accurate, bool file_test); +static void __get_file_information(const char *path, bool file_test); +static void __get_video_frame(const char *path, const char *accurate, bool file_test); static bool __read_file(const char *path, unsigned char **data, unsigned int *size) { @@ -237,18 +236,6 @@ ERROR: return false; } -static bool __is_file_exist(const char *filename) -{ - if (filename) { - const char *to_access = (strstr(filename, "file://") != NULL) ? filename + 7 : filename; - if (access(to_access, R_OK) < 0) { - printf("file [%s] not found.\n", to_access); - return false; - } - } - return true; -} - static int __bulk_test(int argc, char **argv) { GDir *dir = NULL; @@ -270,10 +257,10 @@ static int __bulk_test(int argc, char **argv) path = g_build_path(G_DIR_SEPARATOR_S, argv[1], name, NULL); - if (argv[2] == NULL) - mmfile_get_file_infomation((void *)path, NULL, true); + if (argc < 3) + __get_file_information(path, true); else - mmfile_get_video_frame((void *)path, argv[2], true); + __get_video_frame(path, argv[2], true); g_free(path); } @@ -285,53 +272,43 @@ static int __bulk_test(int argc, char **argv) int main(int argc, char **argv) { - struct stat statbuf; bool file_test = true; /*if you want to test mm_file_create_content_XXX_from_memory() set file_test to false */ if (g_file_test(argv[1], G_FILE_TEST_IS_DIR)) return __bulk_test(argc, argv); - if (__is_file_exist(argv[1])) { - int ret = lstat(argv[1], &statbuf); - if (ret < 0) { - printf("lstat error[%d]\n", ret); - return -1; - } + if (!g_file_test(argv[1], G_FILE_TEST_IS_REGULAR)) + return 0; - if (fpFailList == NULL) - fpFailList = fopen("/opt/var/log/mmfile_fails.txt", "w"); + if (fpFailList == NULL) + fpFailList = fopen("/opt/var/log/mmfile_fails.txt", "w"); - if (argv[2] == NULL) - mmfile_get_file_infomation(argv[1], NULL, file_test); - else - mmfile_get_video_frame(argv[1], argv[2], file_test); + if (argc < 3) + __get_file_information(argv[1], file_test); + else + __get_video_frame(argv[1], argv[2], file_test); - if (fpFailList != NULL) { - fflush(fpFailList); - fclose(fpFailList); - } + if (fpFailList != NULL) { + fflush(fpFailList); + fclose(fpFailList); } return 0;/*exit(0); */ } -static int mmfile_get_file_infomation(void *data, void *user_data, bool file_test) +static void __get_file_information(const char *path, bool file_test) { MMHandleType content_attrs = 0; MMHandleType tag_attrs = 0; int audio_track_num = 0; int video_track_num = 0; int ret = 0; - char filename[512] = {0, }; - - memset(filename, 0x00, sizeof(filename)); - SAFE_STRLCPY(filename, (char *)data, sizeof(filename)); MM_TIME_CHECK_START; - printf("Extracting information for [%s] \n", filename); + printf("Extracting information for [%s] \n", path); /* get track info */ - ret = mm_file_get_stream_info(filename, &audio_track_num, &video_track_num); + ret = mm_file_get_stream_info(path, &audio_track_num, &video_track_num); if (ret == FILEINFO_ERROR_NONE) { printf("# audio=%d, video=%d\n", audio_track_num, video_track_num); } else { @@ -340,16 +317,16 @@ static int mmfile_get_file_infomation(void *data, void *user_data, bool file_tes if (file_test) { /* get content handle */ - ret = mm_file_create_content_attrs(&content_attrs, filename); + ret = mm_file_create_content_attrs(&content_attrs, path); } else { unsigned int file_size = 0; unsigned char *buffer = NULL; /* Read file */ - if (!__read_file(filename, &buffer, &file_size)) { + if (!__read_file(path, &buffer, &file_size)) { printf("Failed to __read_file()\n"); MM_TIME_CHECK_STOP_BY_ERR; - return -1; + return; } ret = mm_file_create_content_attrs_from_memory(&content_attrs, buffer, file_size, MM_FILE_FORMAT_3GP); @@ -425,16 +402,16 @@ static int mmfile_get_file_infomation(void *data, void *user_data, bool file_tes if (file_test) { /* get tag handle */ - ret = mm_file_create_tag_attrs(&tag_attrs, filename); + ret = mm_file_create_tag_attrs(&tag_attrs, path); } else { unsigned int file_size = 0; unsigned char *buffer = NULL; /* Read file */ - if (!__read_file(filename, &buffer, &file_size)) { + if (!__read_file(path, &buffer, &file_size)) { printf("Failed to __read_file()\n"); MM_TIME_CHECK_STOP_BY_ERR; - return -1; + return; } ret = mm_file_create_tag_attrs_from_memory(&tag_attrs, buffer, file_size, MM_FILE_FORMAT_3GP); @@ -508,7 +485,7 @@ static int mmfile_get_file_infomation(void *data, void *user_data, bool file_tes MM_TIME_CHECK_STOP_BY_ERR; mm_file_destroy_tag_attrs(tag_attrs); - return -1; + return; } /* print tag information */ @@ -606,7 +583,7 @@ static int mmfile_get_file_infomation(void *data, void *user_data, bool file_tes if (ret != FILEINFO_ERROR_NONE) { printf("Error mm_file_destroy_tag_attrs: %d", ret); MM_TIME_CHECK_STOP_BY_ERR; - return -1; + return; } } else { printf("Failed to mm_file_create_tag_attrs() error=[%x]\n", ret); @@ -615,48 +592,36 @@ static int mmfile_get_file_infomation(void *data, void *user_data, bool file_tes printf("=================================================\n\n"); - MM_TIME_CHECK_FINISH(filename); - - return 0; + MM_TIME_CHECK_FINISH(path); } -static int mmfile_get_video_frame(void *data, void *accurate, bool file_test) +static void __get_video_frame(const char *path, const char *accurate, bool file_test) { int ret = 0; - char filename[512] = {0, }; - char accurate_mode[10] = {0, }; - void *_frame = NULL; + unsigned char *_frame = NULL; int _frame_size = 0; int width = 0; int height = 0; bool is_accurate = false; unsigned long long time_stamp = 5 * 1000 * 1000; //5sec - memset(filename, 0x00, sizeof(filename)); - SAFE_STRLCPY(filename, (char *)data, sizeof(filename)); + if (accurate && accurate[0] == '1') + is_accurate = true; - memset(accurate_mode, 0x00, sizeof(accurate_mode)); - SAFE_STRLCPY(accurate_mode, (char *)accurate, sizeof(accurate_mode)); + printf("Extracting video frame for [%s] [%llu] accurate [%d]\n", path, time_stamp, is_accurate); - if (strlen(accurate_mode) > 0) { - if (strncmp(accurate_mode, "1", 1) == 0) - is_accurate = true; - } - - printf("Extracting video frame for [%s] [%llu] accurate [%d]\n", filename, time_stamp, is_accurate); - - if (file_test == true) - ret = mm_file_get_video_frame(filename, time_stamp, is_accurate, (unsigned char **)&_frame, &_frame_size, &width, &height); + if (file_test) + ret = mm_file_get_video_frame(path, time_stamp, is_accurate, &_frame, &_frame_size, &width, &height); else { unsigned int file_size = 0; unsigned char *buffer = NULL; /* Read file */ - if (!__read_file(filename, &buffer, &file_size)) { + if (!__read_file(path, &buffer, &file_size)) { printf("Failed to __read_file()\n"); - return -1; + return; } - ret = mm_file_get_video_frame_from_memory(buffer, file_size, time_stamp, is_accurate, (unsigned char **)&_frame, &_frame_size, &width, &height); + ret = mm_file_get_video_frame_from_memory(buffer, file_size, time_stamp, is_accurate, &_frame, &_frame_size, &width, &height); SAFE_FREE(buffer); } @@ -665,5 +630,5 @@ static int mmfile_get_video_frame(void *data, void *accurate, bool file_test) else printf("video_frame[%p], video_frame_len = [%d] width = [%d] height = [%d]\n", _frame, _frame_size, width, height); - return 0; + SAFE_FREE(_frame); } diff --git a/utils/mm_file_util_io.c b/utils/mm_file_util_io.c index 7183327..03698dc 100644 --- a/utils/mm_file_util_io.c +++ b/utils/mm_file_util_io.c @@ -26,6 +26,8 @@ #include "mm_file_debug.h" #include "mm_file_utils.h" +#define HANDLE_STRING_MAX 256 + static unsigned char is_little_endian = 0; inline static int _is_little_endian(void) @@ -140,7 +142,7 @@ int mmfile_open(MMFileIOHandle **handle, const char *filename, int flags) { MMFileIOFunc *pFuc = NULL; const char *pFile = NULL; - char handle_str[256] = {0, }; + char handle_str[HANDLE_STRING_MAX] = {0, }; char *pHandleName = NULL; if (!handle || !filename) { @@ -148,7 +150,7 @@ int mmfile_open(MMFileIOHandle **handle, const char *filename, int flags) return MMFILE_UTIL_FAIL; } - memset(handle_str, 0x00, sizeof(handle_str)); + memset(handle_str, 0x00, HANDLE_STRING_MAX); pFile = filename; pHandleName = handle_str; @@ -159,7 +161,7 @@ int mmfile_open(MMFileIOHandle **handle, const char *filename, int flags) goto file_handle; } - if ((pHandleName - handle_str) < (int)sizeof(handle_str) - 1) { + if ((pHandleName - handle_str) < HANDLE_STRING_MAX - 1) { *pHandleName++ = *pFile; } pFile++; @@ -167,7 +169,8 @@ int mmfile_open(MMFileIOHandle **handle, const char *filename, int flags) if (*pFile == '\0') { file_handle: - SAFE_STRLCPY(handle_str, "file", sizeof(handle_str)); + if (g_strlcpy(handle_str, "file", HANDLE_STRING_MAX) >= HANDLE_STRING_MAX) + debug_error(DEBUG, "truncation occurred."); } else { *pHandleName = '\0'; } @@ -324,4 +327,3 @@ int mmfile_register_io_all() return MMFILE_UTIL_SUCCESS; } - diff --git a/utils/mm_file_util_tag.c b/utils/mm_file_util_tag.c index 499561a..cbec6e6 100644 --- a/utils/mm_file_util_tag.c +++ b/utils/mm_file_util_tag.c @@ -1807,8 +1807,12 @@ int mm_file_get_int_value_from_xml_string(const char* xml_str, const char* param return MMFILE_UTIL_FAIL; } - memset(init_view_ret, 0x00, sizeof(init_view_ret)); - SAFE_STRLCPY(init_view_ret, value_start, sizeof(init_view_ret)); + memset(init_view_ret, 0x00, value_length_max); + if (g_strlcpy(init_view_ret, value_start, value_length_max) >= value_length_max) { + debug_error(DEBUG, "error: truncation occurred"); + return MMFILE_UTIL_FAIL; + } + *value = strtol(init_view_ret, &endptr, 10); if (endptr == init_view_ret) { -- 2.7.4