From 49c1c35982b1e47b4ac4825044df66b94bcba3a1 Mon Sep 17 00:00:00 2001 From: Jakub Karolczyk Date: Fri, 31 Mar 2023 22:07:59 +0100 Subject: [PATCH] [core] Coverity fixes * [core] Coverity CID 1468218 (Resource leak) * [core] Coverity CID 1468214 (Resource leak) * [core] Coverity CID 1294472 (Resource leak) * [core] Coverity CID 1294470 (Resource leak) * [core] Coverity CID 1500361 (Resource leak) * [core] Coverity CID 1500308 (Resource leak) * [core] Coverity CID 1500278 (Resource leak) --------- Co-authored-by: Andrey Volk --- src/include/switch_core_video.h | 2 +- src/mod/applications/mod_cv/mod_cv.cpp | 2 +- .../mod_video_filter/mod_video_filter.c | 2 +- src/mod/formats/mod_imagick/mod_imagick.c | 2 +- src/switch_core_sqldb.c | 2 ++ src/switch_core_video.c | 35 ++++++++++++------- src/switch_event.c | 4 +++ src/switch_xml.c | 20 +++++++++-- 8 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/include/switch_core_video.h b/src/include/switch_core_video.h index 1af0dec7ac..f65c322d30 100644 --- a/src/include/switch_core_video.h +++ b/src/include/switch_core_video.h @@ -448,7 +448,7 @@ SWITCH_DECLARE(switch_status_t) switch_img_to_raw(switch_image_t *src, void *des * \param[in] width The raw data width * \param[in] height The raw data height */ -SWITCH_DECLARE(switch_status_t) switch_img_from_raw(switch_image_t *dest, void *src, switch_img_fmt_t fmt, int width, int height); +SWITCH_DECLARE(switch_status_t) switch_img_from_raw(switch_image_t** destP, void *src, switch_img_fmt_t fmt, int width, int height); SWITCH_DECLARE(switch_image_t *) switch_img_write_text_img(int w, int h, switch_bool_t full, const char *text); SWITCH_DECLARE(switch_image_t *) switch_img_read_file(const char* file_name); diff --git a/src/mod/applications/mod_cv/mod_cv.cpp b/src/mod/applications/mod_cv/mod_cv.cpp index 800452dd9a..de39943b4b 100644 --- a/src/mod/applications/mod_cv/mod_cv.cpp +++ b/src/mod/applications/mod_cv/mod_cv.cpp @@ -854,7 +854,7 @@ static switch_status_t video_thread_callback(switch_core_session_t *session, swi } if (context->rawImage && (context->debug || !context->overlay_count)) { - switch_img_from_raw(frame->img, (uint8_t *)context->rawImage->imageData, SWITCH_IMG_FMT_RGB24, context->rawImage->width, context->rawImage->height); + switch_img_from_raw(&frame->img, (uint8_t *)context->rawImage->imageData, SWITCH_IMG_FMT_RGB24, context->rawImage->width, context->rawImage->height); } int abs = 0; diff --git a/src/mod/applications/mod_video_filter/mod_video_filter.c b/src/mod/applications/mod_video_filter/mod_video_filter.c index 7cf1f1dd0f..605559fdbb 100644 --- a/src/mod/applications/mod_video_filter/mod_video_filter.c +++ b/src/mod/applications/mod_video_filter/mod_video_filter.c @@ -645,7 +645,7 @@ static switch_status_t video_thread_callback(switch_core_session_t *session, swi } - switch_img_from_raw(frame->img, patch_data, SWITCH_IMG_FMT_ARGB, frame->img->d_w, frame->img->d_h); + switch_img_from_raw(&frame->img, patch_data, SWITCH_IMG_FMT_ARGB, frame->img->d_w, frame->img->d_h); switch_img_free(&img); diff --git a/src/mod/formats/mod_imagick/mod_imagick.c b/src/mod/formats/mod_imagick/mod_imagick.c index ce789637cf..beb60387bf 100644 --- a/src/mod/formats/mod_imagick/mod_imagick.c +++ b/src/mod/formats/mod_imagick/mod_imagick.c @@ -387,7 +387,7 @@ static switch_status_t read_page(pdf_file_context_t *context) return SWITCH_STATUS_FALSE; } - switch_img_from_raw(context->img, storage, SWITCH_IMG_FMT_BGR24, w, h); + switch_img_from_raw(&context->img, storage, SWITCH_IMG_FMT_BGR24, w, h); free(storage); } else { switch_image_t *img = switch_img_alloc(NULL, SWITCH_IMG_FMT_ARGB, image->columns, image->rows, 0); diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c index 702ee6a79c..5a75aeb573 100644 --- a/src/switch_core_sqldb.c +++ b/src/switch_core_sqldb.c @@ -3181,6 +3181,8 @@ static int recover_callback(void *pArg, int argc, char **argv, char **columnName if (!(ep = switch_loadable_module_get_endpoint_interface(argv[0]))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "EP ERROR\n"); + switch_xml_free(xml); + return 0; } diff --git a/src/switch_core_video.c b/src/switch_core_video.c index 0d377f9c3e..dad3181c2a 100644 --- a/src/switch_core_video.c +++ b/src/switch_core_video.c @@ -3521,11 +3521,18 @@ SWITCH_DECLARE(switch_status_t) switch_img_to_raw(switch_image_t *src, void *des #endif } -SWITCH_DECLARE(switch_status_t) switch_img_from_raw(switch_image_t *dest, void *src, switch_img_fmt_t fmt, int width, int height) +SWITCH_DECLARE(switch_status_t) switch_img_from_raw(switch_image_t **destP, void *src, switch_img_fmt_t fmt, int width, int height) { #ifdef SWITCH_HAVE_YUV uint32_t fourcc; int ret = -1; + switch_image_t *dest = NULL; + + if (!destP) { + return SWITCH_STATUS_FALSE; + } + + dest = *destP; fourcc = switch_img_fmt2fourcc(fmt); @@ -3574,6 +3581,8 @@ SWITCH_DECLARE(switch_status_t) switch_img_from_raw(switch_image_t *dest, void * 0, fourcc); } + *destP = dest; + return ret == 0 ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; #else return SWITCH_STATUS_FALSE; @@ -3586,10 +3595,12 @@ SWITCH_DECLARE(switch_status_t) switch_img_scale(switch_image_t *src, switch_ima switch_image_t *dest = NULL; int ret = 0; - if (destP) { - dest = *destP; + if (!destP) { + return SWITCH_STATUS_FALSE; } + dest = *destP; + switch_assert(width > 0); switch_assert(height > 0); @@ -3615,15 +3626,13 @@ SWITCH_DECLARE(switch_status_t) switch_img_scale(switch_image_t *src, switch_ima kFilterBox); } + *destP = dest; + if (ret != 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Scaling Error: ret: %d\n", ret); return SWITCH_STATUS_FALSE; } - if (destP) { - *destP = dest; - } - return SWITCH_STATUS_SUCCESS; #else return SWITCH_STATUS_FALSE; @@ -3637,10 +3646,12 @@ SWITCH_DECLARE(switch_status_t) switch_img_mirror(switch_image_t *src, switch_im switch_image_t *dest = NULL; int ret = 0; - if (destP) { - dest = *destP; + if (!destP) { + return SWITCH_STATUS_FALSE; } + dest = *destP; + if (dest && src->fmt != dest->fmt) switch_img_free(&dest); if (!dest) dest = switch_img_alloc(NULL, src->fmt, src->d_w, src->d_h, 1); @@ -3660,15 +3671,13 @@ SWITCH_DECLARE(switch_status_t) switch_img_mirror(switch_image_t *src, switch_im } + *destP = dest; + if (ret != 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Mirror Error: ret: %d\n", ret); return SWITCH_STATUS_FALSE; } - if (destP) { - *destP = dest; - } - return SWITCH_STATUS_SUCCESS; #else return SWITCH_STATUS_FALSE; diff --git a/src/switch_event.c b/src/switch_event.c index 843469bd72..be49f2fc14 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -1909,6 +1909,8 @@ SWITCH_DECLARE(switch_xml_t) switch_event_xmlize(switch_event_t *event, const ch data = (char *) malloc(2048); if (!data) { va_end(ap); + switch_xml_free(xml); + return NULL; } ret = vsnprintf(data, 2048, fmt, ap); @@ -1918,6 +1920,8 @@ SWITCH_DECLARE(switch_xml_t) switch_event_xmlize(switch_event_t *event, const ch #ifndef HAVE_VASPRINTF free(data); #endif + switch_xml_free(xml); + return NULL; } } diff --git a/src/switch_xml.c b/src/switch_xml.c index c43a530252..80b7553907 100644 --- a/src/switch_xml.c +++ b/src/switch_xml.c @@ -1638,11 +1638,25 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_parse_file_simple(const char *file) if ((fd = open(file, O_RDONLY, 0)) > -1) { fstat(fd, &st); - if (!st.st_size) goto error; + if (!st.st_size) { + close(fd); + goto error; + } + m = switch_must_malloc(st.st_size); - if (!(0<(l = read(fd, m, st.st_size)))) goto error; - if (!(root = (switch_xml_root_t) switch_xml_parse_str((char *) m, l))) goto error; + if (!(0 < (l = read(fd, m, st.st_size)))) { + free(m); + close(fd); + goto error; + } + + if (!(root = (switch_xml_root_t)switch_xml_parse_str((char*)m, l))) { + free(m); + close(fd); + goto error; + } + root->dynamic = 1; close(fd); return &root->xml;