src: Fix invalid return check for g_malloc 72/134272/1
authorSyam Sidhardhan <s.syam@samsung.com>
Thu, 15 Jun 2017 15:07:34 +0000 (20:37 +0530)
committerSyam Sidhardhan <s.syam@samsung.com>
Thu, 15 Jun 2017 15:07:40 +0000 (20:37 +0530)
if params are correct, g_malloc*() will always return
a valid pointer, otherwise it will assert.

Change-Id: Ia2140202e8bce62a527af2fa8596d0e3e4e42c9b
Signed-off-by: Syam Sidhardhan <s.syam@samsung.com>
src/bluetooth-adapter.c
src/bluetooth-audio.c
src/bluetooth-avrcp.c
src/bluetooth-common.c
src/bluetooth-gatt.c
test/bt_unit_test.c

index a4ddad5f42073efc3b8d37dce15164a124eab8e2..4abca1b33519ca18bd5ad0b90b17a43fc6b5c1da 100644 (file)
@@ -1588,11 +1588,6 @@ int bt_adapter_le_create_advertiser(bt_advertiser_h *advertiser)
        BT_CHECK_INPUT_PARAMETER(advertiser);
 
        __adv = (bt_advertiser_s *)g_malloc0(sizeof(bt_advertiser_s));
-       if (__adv == NULL) {
-               BT_ERR("OUT_OF_MEMORY(0x%08x)",
-                               BT_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
-               return BT_ERROR_OUT_OF_MEMORY; /* LCOV_EXCL_LINE */
-       }
        __adv->handle = GPOINTER_TO_INT(__adv);
 
        *advertiser = (bt_advertiser_h)__adv;
@@ -1826,8 +1821,6 @@ static int __bt_convert_string_to_uuid(const char *string,
                unsigned short val;
                char *stop;
                data = g_malloc0(sizeof(char) *2);
-               if (data == NULL)
-                       return BT_ERROR_OUT_OF_MEMORY; /* LCOV_EXCL_LINE */
 
                val = strtol(string, &stop, 16);
                val = htons(val);
@@ -1850,9 +1843,6 @@ static int __bt_convert_string_to_uuid(const char *string,
                unsigned int val0, val4;
                unsigned short val1, val2, val3, val5;
                data = g_malloc0(sizeof(char) *16);
-               /* Fix : NULL_RETURNS */
-               if (data == NULL)
-                       return BT_ERROR_OUT_OF_MEMORY;
 
                /* UUID format : %08x-%04hx-%04hx-%04hx-%08x%04hx */
                strncpy(str_ptr, string, 36);
@@ -1914,9 +1904,6 @@ static int __bt_convert_byte_ordering(char *data, int data_len,
 
        /* Convert to little endian */
        swp = g_malloc0(data_len);
-       /* Fix : NULL_RETURNS */
-       if (swp == NULL)
-               return BT_ERROR_OUT_OF_MEMORY;
        for (i = 0, j = data_len - 1; i < data_len; i++, j--)
                swp[i] = data[j];
 
@@ -2011,8 +1998,6 @@ static int __bt_append_adv_type_data(bt_advertiser_h advertiser,
        }
 
        new_adv = g_malloc0(adv_len + new_data_len);
-       if (!new_adv)
-               return BT_ERROR_OUT_OF_MEMORY;
 
        for (i = 0; i < adv_len; i++) {
                len = adv_data[i];
@@ -2189,10 +2174,6 @@ int bt_adapter_le_add_advertising_service_data(bt_advertiser_h advertiser,
        g_free(uuid_ptr);
 
        adv_data = g_malloc0(sizeof(char) *(service_data_len + 2));
-       if (!adv_data) {
-               g_free(converted_uuid); /* LCOV_EXCL_LINE */
-               return BT_ERROR_OUT_OF_MEMORY;
-       }
 
        memcpy(adv_data, converted_uuid, 2);
        memcpy(adv_data + 2, service_data, service_data_len);
@@ -2254,8 +2235,6 @@ int bt_adapter_le_add_advertising_manufacturer_data(bt_advertiser_h advertiser,
        }
 
        adv_data = g_malloc0(sizeof(char) *(manufacturer_data_len + 2));
-       if (!adv_data)
-               return BT_ERROR_OUT_OF_MEMORY;
 
        adv_data[0] = manufacturer_id & 0xffffffff;
        adv_data[1] = (manufacturer_id & 0xff00) >> 8;
@@ -2913,9 +2892,6 @@ int bt_adapter_le_get_scan_result_service_uuids(const bt_adapter_le_device_scan_
                return BT_ERROR_NO_DATA;
 
        *uuids = g_malloc0(sizeof(char *) *uuid_count);
-       /* Fix : NULL_RETURNS */
-       if (*uuids == NULL)
-               return BT_ERROR_OUT_OF_MEMORY;
 
        *count = uuid_count;
 
@@ -2936,19 +2912,13 @@ int bt_adapter_le_get_scan_result_service_uuids(const bt_adapter_le_device_scan_
                if (uuid_size != 0) {
                        for (i = 0; i < (field_len - 1); i += uuid_size) {
                                if (uuid_size == 2) {
-                                       (*uuids)[uuid_index] = g_malloc0(sizeof(char) *4 + 1);
-                                       /* Fix : NULL_RETURNS */
-                                       if ((*uuids)[uuid_index] == NULL)
-                                               return BT_ERROR_OUT_OF_MEMORY;
+                                       (*uuids)[uuid_index] = g_malloc0(sizeof(char) * 4 + 1);
                                        snprintf((*uuids)[uuid_index], 5,
                                                        "%2.2X%2.2X",
                                                        remain_data[i+3],
                                                        remain_data[i+2]);
                                } else {
-                                       (*uuids)[uuid_index] = g_malloc0(sizeof(char) *36 + 1);
-                                       /* Fix : NULL_RETURNS */
-                                       if ((*uuids)[uuid_index] == NULL)
-                                               return BT_ERROR_OUT_OF_MEMORY;
+                                       (*uuids)[uuid_index] = g_malloc0(sizeof(char) * 36 + 1);
                                        snprintf((*uuids)[uuid_index], 37, "%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
                                                remain_data[i+17], remain_data[i+16], remain_data[i+15], remain_data[i+14],
                                                remain_data[i+13], remain_data[i+12], remain_data[i+11], remain_data[i+10], remain_data[i+9], remain_data[i+8],
@@ -2994,9 +2964,6 @@ int bt_adapter_le_get_scan_result_device_name(const bt_adapter_le_device_scan_re
                if (adv_data[1] == BT_ADAPTER_LE_ADVERTISING_DATA_LOCAL_NAME ||
                        adv_data[1] == BT_ADAPTER_LE_ADVERTISING_DATA_SHORT_LOCAL_NAME) {
                        *name = g_malloc0(sizeof(char) *field_len);
-                       /* Fix : NULL_RETURNS */
-                       if (*name == NULL)
-                               return BT_ERROR_OUT_OF_MEMORY;
                        memcpy(*name, &adv_data[2], field_len - 1);
 
                        return BT_ERROR_NONE;
@@ -3100,10 +3067,7 @@ int bt_adapter_le_get_scan_result_service_solicitation_uuids(const bt_adapter_le
        if (uuid_count == 0)
                return BT_ERROR_NO_DATA;
 
-       *uuids = g_malloc0(sizeof(char *) *uuid_count);
-       /* Fix : NULL_RETURNS */
-       if (*uuids == NULL)
-               return BT_ERROR_OUT_OF_MEMORY;
+       *uuids = g_malloc0(sizeof(char *) * uuid_count);
        *count = uuid_count;
 
        remain_data = adv_data;
@@ -3121,18 +3085,12 @@ int bt_adapter_le_get_scan_result_service_solicitation_uuids(const bt_adapter_le
                if (uuid_size != 0) {
                        for (i = 0; i < (field_len - 1); i += uuid_size) {
                                if (uuid_size == 2) {
-                                       (*uuids)[uuid_index] = g_malloc0(sizeof(char) *4 + 1);
-                                       /* Fix : NULL_RETURNS */
-                                       if ((*uuids)[uuid_index] == NULL)
-                                               return BT_ERROR_OUT_OF_MEMORY;
+                                       (*uuids)[uuid_index] = g_malloc0(sizeof(char) * 4 + 1);
                                        snprintf((*uuids)[uuid_index], 5,
                                                "%2.2X%2.2X", remain_data[i+3],
                                                remain_data[i+2]);
                                } else {
-                                       (*uuids)[uuid_index] = g_malloc0(sizeof(char) *36 + 1);
-                                       /* Fix : NULL_RETURNS */
-                                       if ((*uuids)[uuid_index] == NULL)
-                                               return BT_ERROR_OUT_OF_MEMORY;
+                                       (*uuids)[uuid_index] = g_malloc0(sizeof(char) * 36 + 1);
                                        snprintf((*uuids)[uuid_index], 37, "%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
                                                remain_data[i+17], remain_data[i+16], remain_data[i+15], remain_data[i+14],
                                                remain_data[i+13], remain_data[i+12], remain_data[i+11], remain_data[i+10], remain_data[i+9], remain_data[i+8],
@@ -3193,10 +3151,7 @@ int bt_adapter_le_get_scan_result_service_data_list(const bt_adapter_le_device_s
        if (data_count == 0)
                return BT_ERROR_NO_DATA;
 
-       *data_list = g_malloc0(sizeof(bt_adapter_le_service_data_s) *data_count);
-       /* Fix : NULL_RETURNS */
-       if (*data_list == NULL)
-               return BT_ERROR_OUT_OF_MEMORY;
+       *data_list = g_malloc0(sizeof(bt_adapter_le_service_data_s) * data_count);
        *count = data_count;
 
        remain_data = adv_data;
@@ -3373,18 +3328,12 @@ int bt_adapter_le_get_scan_result_ibeacon_report(const bt_adapter_le_device_scan
                return BT_ERROR_NO_DATA;
 
        *ibeacon_info = g_malloc0(sizeof(bt_adapter_le_ibeacon_scan_result_info_s));
-       if (*ibeacon_info == NULL)
-               return BT_ERROR_OUT_OF_MEMORY;
 
        (*ibeacon_info)->company_id = manufacturer_id;
        (*ibeacon_info)->ibeacon_type = manufacturer_data[1] << 8;
        (*ibeacon_info)->ibeacon_type += manufacturer_data[0];
 
-       (*ibeacon_info)->uuid = g_malloc0(sizeof(char) *36 + 1);
-       if ((*ibeacon_info)->uuid == NULL) {
-               g_free(*ibeacon_info);
-               return BT_ERROR_OUT_OF_MEMORY;
-       }
+       (*ibeacon_info)->uuid = g_malloc0(sizeof(char) * 36 + 1);
 
        snprintf((*ibeacon_info)->uuid, 37, "%2.2X%2.2X%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X-%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
                manufacturer_data[17], manufacturer_data[16], manufacturer_data[15], manufacturer_data[14],
@@ -3413,11 +3362,6 @@ int bt_adapter_le_scan_filter_create(bt_scan_filter_h *scan_filter)
        BT_CHECK_INPUT_PARAMETER(scan_filter);
 
        __filter = (bt_le_scan_filter_s *)g_malloc0(sizeof(bt_le_scan_filter_s));
-       if (__filter == NULL) {
-               BT_ERR("OUT_OF_MEMORY(0x%08x)",
-                               BT_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
-               return BT_ERROR_OUT_OF_MEMORY;
-       }
 
        __filter->manufacturer_id = -1;
        *scan_filter = (bt_scan_filter_h)__filter;
index 218da209f34461f3764a09614d7218b69d1b7775..0df98ae74f6c83d62cf77543a1deb7f68a9dd896 100644 (file)
@@ -791,10 +791,6 @@ int bt_call_list_create(bt_call_list_h *list)
                return BT_ERROR_ALREADY_DONE;
        }
        handle = g_malloc0(sizeof(call_list_s));
-       if (handle == NULL) {
-               BT_ERR("BT_ERROR_OUT_OF_MEMORY(0x%08x)", BT_ERROR_OUT_OF_MEMORY);
-               return BT_ERROR_OUT_OF_MEMORY;
-       }
 
        *list = handle;
        return BT_ERROR_NONE;
@@ -852,9 +848,6 @@ int bt_call_list_add(bt_call_list_h list, unsigned int call_id, bt_ag_call_state
        handle = (call_list_s *)list;
        call_status = g_malloc0(sizeof(bt_telephony_call_status_info_t));
 
-       /* Fix : NULL_RETURNS */
-       if (call_status == NULL)
-               return BT_ERROR_OUT_OF_MEMORY;
        call_status->call_id = call_id;
        call_status->call_status = state;
        if (phone_number)
index 6a73b44593ad5b27fd2fba91938659e67d075742..c80a19705e796f02a8cfc8615cf8c6c0b28b0604 100644 (file)
@@ -573,16 +573,15 @@ int bt_avrcp_control_get_track_info(bt_avrcp_metadata_attributes_info_s **track)
        } else {
                tr_info = (bt_avrcp_metadata_attributes_info_s *)g_malloc0(
                                sizeof(bt_avrcp_metadata_attributes_info_s));
-               if (tr_info) {
-                       tr_info->title = metadata.title;
-                       tr_info->artist = metadata.artist;
-                       tr_info->album = metadata.album;
-                       tr_info->genre = metadata.genre;
-                       tr_info->total_tracks = metadata.total_tracks;
-                       tr_info->number = metadata.number;
-                       tr_info->duration = metadata.duration;
-                       *track = tr_info;
-               }
+
+               tr_info->title = metadata.title;
+               tr_info->artist = metadata.artist;
+               tr_info->album = metadata.album;
+               tr_info->genre = metadata.genre;
+               tr_info->total_tracks = metadata.total_tracks;
+               tr_info->number = metadata.number;
+               tr_info->duration = metadata.duration;
+               *track = tr_info;
        }
        return error;
 }
index 2ea0c38f40c2f5df58e8812521e2b357b8e3646e..c9594f8c86dff2fa6894acd57d2fc29d25459492 100644 (file)
@@ -674,10 +674,6 @@ static int __bt_get_bt_device_sdp_info_s(bt_device_sdp_info_s **dest, bt_sdp_inf
 
        *dest = (bt_device_sdp_info_s *)g_malloc0(sizeof(bt_device_sdp_info_s));
 
-       /* Fix : NULL_RETURNS */
-       if (*dest == NULL)
-               return BT_ERROR_OUT_OF_MEMORY;
-
        if (_bt_convert_address_to_string(&((*dest)->remote_address), &(source->device_addr)) != BT_ERROR_NONE) {
                __bt_free_bt_device_sdp_info_s(*dest);
                return BT_ERROR_OUT_OF_MEMORY;
@@ -735,8 +731,6 @@ static void __bt_free_bt_device_sdp_info_s(bt_device_sdp_info_s *sdp_info)
 static int __bt_get_bt_gatt_client_att_mtu_info_s(bt_gatt_client_att_mtu_info_s **dest, bluetooth_le_att_mtu_info_t *source)
 {
        *dest = (bt_gatt_client_att_mtu_info_s *)g_malloc0(sizeof(bt_gatt_client_att_mtu_info_s));
-       if (*dest == NULL)
-               return BT_ERROR_OUT_OF_MEMORY;
 
        if (_bt_convert_address_to_string(&((*dest)->remote_address), &(source->device_address)) != BT_ERROR_NONE) {
                g_free(*dest);
@@ -755,10 +749,6 @@ static int __bt_get_bt_device_connection_info_s(bt_device_connection_info_s **de
 {
        *dest = (bt_device_connection_info_s *)g_malloc0(sizeof(bt_device_connection_info_s));
 
-       /* Fix : NULL_RETURNS */
-       if (*dest == NULL)
-               return BT_ERROR_OUT_OF_MEMORY;
-
        if (_bt_convert_address_to_string(&((*dest)->remote_address), &(source->device_addr)) != BT_ERROR_NONE) {
                __bt_free_bt_device_connection_info_s(*dest);
                return BT_ERROR_OUT_OF_MEMORY;
@@ -2635,8 +2625,7 @@ static void __bt_event_proxy(int event, bluetooth_event_param_t *param, void *us
                if (device_addr != NULL)
                        free(device_addr);
 
-               if (vcard_info != NULL)
-                       free(vcard_info);
+               g_free(vcard_info);
 
                break;
        } else {
@@ -3089,10 +3078,7 @@ static void __bt_le_event_proxy(int event, bluetooth_event_param_t *param, void
                                }
                                __bt_free_tds_scan_result_info_s(info);
 
-                               if (data) {
-                                       g_free(data);
-                                       data = NULL;
-                               }
+                               g_free(data);
                        }
                }
 
index 52f9632495a9660b9edcff5b9cf7fd2263b18a33..f51e799362775ccdb79ec2e617920577ea138a9e 100644 (file)
@@ -1371,22 +1371,12 @@ int bt_gatt_set_int_value(bt_gatt_h gatt_handle, bt_data_type_int_e type,
                *val_len = fmt_size;
        } else if (*val_len == offset) { /* Added */
                tmp = g_malloc0(*val_len + fmt_size);
-               /* Fix : NULL_RETURNS */
-               if (!tmp) {
-                       g_free(*val);
-                       return BT_ERROR_OUT_OF_MEMORY;
-               }
                memcpy(tmp, *val, *val_len);
                g_free(*val);
                *val = tmp;
                *val_len += fmt_size;
        } else if (*val_len < offset + fmt_size) {/* Overlapped */
                tmp = g_malloc0(offset + fmt_size);
-               /* Fix : NULL_RETURNS */
-               if (!tmp) {
-                       g_free(*val);
-                       return BT_ERROR_OUT_OF_MEMORY;
-               }
                memcpy(tmp, *val, *val_len);
                g_free(*val);
                *val = tmp;
@@ -1496,28 +1486,15 @@ int bt_gatt_set_float_value(bt_gatt_h gatt_handle, bt_data_type_float_e type,
 
        if (*val == NULL) {
                *val = g_malloc0(fmt_size);
-               /* Fix : NULL_RETURNS */
-               if (*val == NULL)
-                       return BT_ERROR_OUT_OF_MEMORY;
                *val_len = fmt_size;
        } else if (*val_len == offset) {/* Added */
                tmp = g_malloc0(*val_len + fmt_size);
-               /* Fix : NULL_RETURNS */
-               if (tmp == NULL) {
-                       g_free(*val);
-                       return BT_ERROR_OUT_OF_MEMORY;
-               }
                memcpy(tmp, *val, *val_len);
                g_free(*val);
                *val = tmp;
                *val_len += fmt_size;
        } else if (*val_len < offset + fmt_size) {/* Overlapped */
                tmp = g_malloc0(offset + fmt_size);
-               /* Fix : NULL_RETURNS */
-               if (tmp == NULL) {
-                       g_free(*val);
-                       return BT_ERROR_OUT_OF_MEMORY;
-               }
                memcpy(tmp, *val, *val_len);
                g_free(*val);
                *val = tmp;
index 9ff394c46ce40dc922d369986a4791632fa2cc85..f39614c7283e98ad2da35bb30fea11343e9e2081 100644 (file)
@@ -2310,11 +2310,6 @@ void __bt_gatt_client_print_value(bt_gatt_h h)
 
        if (hex_format) {
                str = g_malloc0(len * 5 + 1);
-               /* Fix : NULL_RETURNS */
-               if (!str) {
-                       TC_PRT("BT_ERROR_OUT_OF_MEMORY");
-                       goto fail;
-               }
                for (i = 0; i < len; i++) {
                        if (i > 0)
                                str[i * 5 - 1] = ' ';
@@ -2323,16 +2318,11 @@ void __bt_gatt_client_print_value(bt_gatt_h h)
                }
        } else {
                str = g_malloc0(len + 1);
-               /* Fix : NULL_RETURNS */
-               if (!str) {
-                       TC_PRT("BT_ERROR_OUT_OF_MEMORY");
-                       goto fail;
-               }
                memcpy(str, value, len);
        }
 
        TC_PRT("Value : %s", str);
-fail:
+
        g_free(str);
        g_free(value);
 }