Fix Coverity issues (incorrect deallocator used) 08/224408/7 accepted/tizen_6.0_unified_hotfix tizen_6.0_hotfix accepted/tizen/6.0/unified/20201030.122138 accepted/tizen/6.0/unified/hotfix/20201103.004443 accepted/tizen/6.0/unified/hotfix/20201103.051557 accepted/tizen/unified/20200214.120051 submit/tizen/20200214.023253 submit/tizen_6.0/20201029.205102 submit/tizen_6.0_hotfix/20201102.192502 submit/tizen_6.0_hotfix/20201103.114802 tizen_6.0.m2_release
authorGilbok Lee <gilbok.lee@samsung.com>
Mon, 10 Feb 2020 09:06:02 +0000 (18:06 +0900)
committerGilbok Lee <gilbok.lee@samsung.com>
Thu, 13 Feb 2020 08:27:37 +0000 (17:27 +0900)
- Remove Null check after using g_malloc

[Version] 0.1.35
[Issue Type] Fix coverity issue

Change-Id: I25d508d42a8e65b9cd87b59044c5050a3917ddc3

packaging/capi-mediademuxer.spec
src/mediademuxer.c
src/mediademuxer_port.c
src/port_gst/mediademuxer_port_gst.c
test/mediademuxer_test.c

index 3846087..6680dba 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-mediademuxer
 Summary:    A Media Demuxer library in Tizen Native API
-Version:    0.1.34
+Version:    0.1.35
 Release:    1
 Group:      Multimedia/API
 License:    Apache-2.0
index 03ed360..e2e7ae8 100644 (file)
@@ -39,28 +39,25 @@ int mediademuxer_create(mediademuxer_h *demuxer)
        mediademuxer_error_e ret;
        DEMUXER_INSTANCE_CHECK(demuxer);
 
-       mediademuxer_s *handle;
+       mediademuxer_s *handle = NULL;
        if (*demuxer == NULL) {
-               handle = (mediademuxer_s *) g_malloc(sizeof(mediademuxer_s));
-               if (handle != NULL) {
-                       memset(handle, 0, sizeof(mediademuxer_s));
-                       handle->demux_state = MEDIADEMUXER_STATE_NONE;
-               } else {
+               handle = (mediademuxer_s *) g_try_malloc0(sizeof(mediademuxer_s));
+               if (!handle) {
                        MD_E("[CoreAPI][%s] DEMUXER_ERROR_OUT_OF_MEMORY(0x%08x)",
                                __FUNCTION__, MEDIADEMUXER_ERROR_OUT_OF_MEMORY);
                        return MEDIADEMUXER_ERROR_OUT_OF_MEMORY;
                }
+               handle->demux_state = MEDIADEMUXER_STATE_NONE;
        } else {
                MD_E("Already created the instance\n");
                return MEDIADEMUXER_ERROR_INVALID_OPERATION;
        }
 
        ret = md_create(&handle->md_handle);
-
        if (ret != MEDIADEMUXER_ERROR_NONE) {
                MD_E("[CoreAPI][%s] DEMUXER_ERROR_INVALID_OPERATION(0x%08x)",
                        __FUNCTION__, MEDIADEMUXER_ERROR_INVALID_OPERATION);
-               free(handle);
+               g_free(handle);
                handle = NULL;
                return MEDIADEMUXER_ERROR_INVALID_OPERATION;
        } else {
index 7381ce8..e49c3cb 100644 (file)
@@ -61,14 +61,11 @@ int md_create(MMHandleType *demuxer)
        media_port_demuxer_ops *pOps = NULL;
        md_handle_t *new_demuxer = NULL;
        MEDIADEMUXER_FENTER();
-       new_demuxer = (md_handle_t *)g_malloc(sizeof(md_handle_t));
+       new_demuxer = (md_handle_t *)g_malloc0(sizeof(md_handle_t));
        MD_I("md_create allocatiing new_demuxer %p:\n", new_demuxer);
-       MEDIADEMUXER_CHECK_NULL(new_demuxer);
-       memset(new_demuxer, 0, sizeof(md_handle_t));
 
        /* alloc ops structure */
-       pOps = (media_port_demuxer_ops *)g_malloc(sizeof(media_port_demuxer_ops));
-       MEDIADEMUXER_CHECK_NULL(pOps);
+       pOps = (media_port_demuxer_ops *)g_malloc0(sizeof(media_port_demuxer_ops));
 
        new_demuxer->demuxer_ops = pOps;
        MD_I("md_create allocating new_demuxer->demuxer_ops %p:\n",
@@ -91,10 +88,8 @@ int md_create(MMHandleType *demuxer)
        return result;
 ERROR:
        *demuxer = (MMHandleType)0;
-       if (pOps)
-               g_free(pOps);
-       if (new_demuxer)
-               g_free(new_demuxer);
+       g_free(pOps);
+       g_free(new_demuxer);
        MEDIADEMUXER_FLEAVE();
        return result;
 }
@@ -215,14 +210,13 @@ mediademuxer_src_type __md_util_media_type(char **uri)
                file_stat = __md_util_exist_file_path(*uri);
                if (file_stat == MD_ERROR_NONE) {
                        int len_uri = strlen(*uri);
-                       old_uristr = (char *)g_malloc(sizeof(char) * (len_uri + 1));
+                       old_uristr = (char *)g_strdup(*uri);
                        MEDIADEMUXER_CHECK_NULL(old_uristr);
-                       MD_L("allocating temp old_uristr[%p] \n", old_uristr);
-                       strncpy(old_uristr, *uri, len_uri + 1);
                        /* need to added 7 char for file:// + 1 for '\0'+ uri len */
+                       MD_L("allocating temp old_uristr[%p] \n", old_uristr);
                        new_uristr = (char *)realloc(*uri, (7 + len_uri + 1) * sizeof(char));
                        if (!new_uristr) {
-                               free(old_uristr);
+                               g_free(old_uristr);
                                old_uristr = NULL;
                                return MD_ERROR_INVALID_ARGUMENT;
                        }
@@ -230,7 +224,7 @@ mediademuxer_src_type __md_util_media_type(char **uri)
                        *uri = new_uristr;
                        g_snprintf(*uri, 7 + len_uri + 1, "file://%s", old_uristr);
                        MD_L("deallocating old_uristr[%p] \n", old_uristr);
-                       free(old_uristr);
+                       g_free(old_uristr);
                        old_uristr = NULL;
                        if (__md_util_is_sdp_file((char *)(*uri))) {
 #ifdef TIZEN_FEATURE_STREAMING
@@ -257,17 +251,14 @@ ERROR:
 int _md_util_parse(MMHandleType demuxer, const char *type)
 {
        char *media_type_string = NULL;
-       int lenght_string = 0;
        int result = MD_ERROR_NONE;
        md_handle_t *new_demuxer = NULL;
        MEDIADEMUXER_FENTER();
        MEDIADEMUXER_CHECK_NULL(demuxer);
        new_demuxer = (md_handle_t *)demuxer;
-       lenght_string = strlen(type);
-       media_type_string = (char *)g_malloc(sizeof(char) * (lenght_string + 1));
+       media_type_string = (char *)g_strdup(type);
        MEDIADEMUXER_CHECK_NULL(media_type_string);
        MD_L("media_type_string allocating %p\n", media_type_string);
-       strncpy(media_type_string, type, lenght_string + 1);
        /*Set media_type depending upon the header of string else consider using file protocol */
        if (new_demuxer->uri_src) {
                MD_L("new_demuxer->uri_src deallocating %p\n", new_demuxer->uri_src);
@@ -276,8 +267,7 @@ int _md_util_parse(MMHandleType demuxer, const char *type)
        }
        new_demuxer->uri_src_media_type = __md_util_media_type(&media_type_string);
        if (new_demuxer->uri_src_media_type != MEDIADEMUXER_SRC_INVALID) {
-               if (new_demuxer->uri_src)
-                       g_free(new_demuxer->uri_src);
+               g_free(new_demuxer->uri_src);
                new_demuxer->uri_src = media_type_string;
                MD_L("uri:%s\n uri_type:%d\n", new_demuxer->uri_src,
                     new_demuxer->uri_src_media_type);
index afc45c4..7f28818 100644 (file)
@@ -260,14 +260,13 @@ static int gst_demuxer_init(MMHandleType *pHandle)
        MEDIADEMUXER_FENTER();
        int ret = MD_ERROR_NONE;
        mdgst_handle_t *new_mediademuxer = NULL;
-       new_mediademuxer = (mdgst_handle_t *)g_malloc(sizeof(mdgst_handle_t));
-       MD_I("gst_demuxer_init allocating new_mediademuxer:%p\n", new_mediademuxer);
+       new_mediademuxer = (mdgst_handle_t *)g_try_malloc0(sizeof(mdgst_handle_t));
        if (!new_mediademuxer) {
                MD_E("Cannot allocate memory for demuxer\n");
                ret = MD_ERROR;
                goto ERROR;
        }
-       memset(new_mediademuxer, 0, sizeof(mdgst_handle_t));
+       MD_I("gst_demuxer_init allocating new_mediademuxer:%p\n", new_mediademuxer);
        new_mediademuxer->is_prepared = false;
        (new_mediademuxer->info).num_audio_track = 0;
        (new_mediademuxer->info).num_video_track = 0;
@@ -332,21 +331,12 @@ static void __gst_no_more_pad(GstElement *element, gpointer data)
 {
        MEDIADEMUXER_FENTER();
        mdgst_handle_t *gst_handle = (mdgst_handle_t *)data;
-       int loop_track;
        track_info *head_track = &(gst_handle->info);
        track *head = head_track->head;
        MEDIADEMUXER_CHECK_NULL_VOID(gst_handle);
        gst_handle->selected_tracks =
-           (bool *) g_malloc(sizeof(bool) * (gst_handle->total_tracks));
+           (bool *) g_malloc0_n(sizeof(bool), gst_handle->total_tracks);
        MD_I("Allocating %p to core->selected_tracks \n", gst_handle->selected_tracks);
-       if (!gst_handle->selected_tracks) {
-               MD_E("[%s]Memory allocation failed\n", __FUNCTION__);
-               return;
-       } else {
-               for (loop_track = 0; loop_track < gst_handle->total_tracks;
-                    loop_track++)
-                       gst_handle->selected_tracks[loop_track] = false;
-       }
        MD_I("Number of video tracks are %d\n", head_track->num_video_track);
        MD_I("Number of audio tracks are %d\n", head_track->num_audio_track);
        MD_I("Number of subtitle tracks are %d\n",
@@ -388,11 +378,11 @@ void __gst_free_stuct(track **head)
                if (temp->next) {
                        track *next = temp->next;
                        MD_I("deallocate memory %p\n", temp);
-                       free(temp);
+                       g_free(temp);
                        temp = next;
                } else {
                        MD_I("deallocate memory %p\n", temp);
-                       free(temp);
+                       g_free(temp);
                        temp = NULL;
                        *head = NULL;
                }
@@ -415,14 +405,8 @@ int __gst_add_track_info(GstPad *pad, GstCaps *caps, track **head,
        GstPad *id3_srcpad = NULL;
        track *temp = NULL;
 
-       temp = (track *)(g_malloc(sizeof(track)));
-       if (!temp) {
-               MD_E("Not able to allocate memory");
-               return MD_ERROR;
-       } else {
-               MD_I("allocate memory %p", temp);
-       }
-       memset(temp, 0x00, sizeof(track));
+       temp = (track *)(g_malloc0(sizeof(track)));
+       MD_I("allocate memory %p", temp);
 
        temp->pad = gst_object_ref(pad);
        temp->caps = gst_caps_ref(caps);
index 526c6b7..3621172 100644 (file)
@@ -486,11 +486,7 @@ static void mediacodec_process_video_pkt(media_packet_h in_buf)
 void *_fetch_audio_data(void *ptr)
 {
        int ret = MEDIADEMUXER_ERROR_NONE;
-       int *status = (int *)g_malloc(sizeof(int) * 1);
-       if (!status) {
-               g_print("Fail malloc fetch audio data retur status value\n");
-               return NULL;
-       }
+       int *status = (int *)g_malloc(sizeof(int));
        media_packet_h audbuf;
        int count = 0;
        uint64_t buf_size = 0;
@@ -610,11 +606,7 @@ static void _local_media_packet_get_codec_data(media_packet_h pkt)
 void *_fetch_video_data(void *ptr)
 {
        int ret = MEDIADEMUXER_ERROR_NONE;
-       int *status = (int *)g_malloc(sizeof(int) * 1);
-       if (!status) {
-               g_print("Fail malloc fetch video data retur status value\n");
-               return NULL;
-       }
+       int *status = (int *)g_malloc(sizeof(int));
        media_packet_h vidbuf;
        int count = 0;
        uint64_t buf_size = 0;
@@ -686,11 +678,7 @@ void *_fetch_video_data(void *ptr)
 void *_fetch_text_data(void *ptr)
 {
        int ret = MEDIADEMUXER_ERROR_NONE;
-       int *status = (int *)g_malloc(sizeof(int) * 1);
-       if (!status) {
-               g_print("Fail malloc fetch video data retur status value\n");
-               return NULL;
-       }
+       int *status = (int *)g_malloc(sizeof(int));
        media_packet_h txtbuf;
        int count = 0;
        uint64_t buf_size = 0;