Fixed chances of memory corruption in wifi_manager_get_mac_address(). 11/91011/1
authorNishant Chaprana <n.chaprana@samsung.com>
Wed, 5 Oct 2016 09:20:08 +0000 (14:50 +0530)
committerNishant Chaprana <n.chaprana@samsung.com>
Wed, 5 Oct 2016 09:39:17 +0000 (15:09 +0530)
Description: As per API documentation, mac_address should be freed using free().
But we are using g_try_malloc0 for allocating memory for Tizen TV profile.
So, that allocated memory should be freed by g_free() only as per GLIB documentation.

Usage os g_try_malloc0 in API will lead to memory corruption, because g_try_malloc0
and normal memory allocator may use different memory pools which will be freed properly
by there corresponding memory deallocator only.

So to match declaration in header file for wifi_manager_get_mac_address()
we should use strndup in place of g_try_malloc0().

Change-Id: I4263a456c9f6fa7dec0ce4cd8c24778b4ae6e850
Signed-off-by: Nishant Chaprana <n.chaprana@samsung.com>
packaging/capi-network-wifi-manager.spec
src/wifi_manager.c
test/wifi_manager_test.c

index 61f1d6d..e667efb 100755 (executable)
@@ -1,6 +1,6 @@
 Name:          capi-network-wifi-manager
 Summary:       Network Wi-Fi library in TIZEN C API
-Version:       1.0.6
+Version:       1.0.7
 Release:       1
 Group:         System/Network
 License:       Apache-2.0
index e7ed776..9e23c75 100755 (executable)
@@ -285,13 +285,13 @@ EXPORT_API int wifi_manager_get_mac_address(wifi_manager_h wifi, char **mac_addr
 
        WIFI_LOG(WIFI_INFO, "%s : %s\n", WIFI_MAC_ADDR_PATH, buf);
 
-       *mac_address = (char *)g_try_malloc0(WIFI_MAC_ADDR_LENGTH + 1);
+       *mac_address = strndup(buf, WIFI_MAC_ADDR_LENGTH + 1);
        if (*mac_address == NULL) {
-               WIFI_LOG(WIFI_ERROR, "malloc() failed"); //LCOV_EXCL_LINE
+               WIFI_LOG(WIFI_ERROR, "strndup() failed"); //LCOV_EXCL_LINE
                fclose(fp); //LCOV_EXCL_LINE
                return WIFI_MANAGER_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
        }
-       g_strlcpy(*mac_address, buf, WIFI_MAC_ADDR_LENGTH + 1);
+
        fclose(fp);
 #else
        *mac_address = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
index d29c6b7..fb04a21 100755 (executable)
@@ -1209,7 +1209,7 @@ int test_wifi_manager_get_mac_address(void)
        }
 
        printf("MAC address : %s\n", mac_addr);
-       g_free(mac_addr);
+       free(mac_addr);
 
        return 1;
 }