telephony: Fixed memory leak 81/180881/2
authorSaurav Babu <saurav.babu@samsung.com>
Tue, 5 Jun 2018 07:27:31 +0000 (12:57 +0530)
committerSaurav Babu <saurav.babu@samsung.com>
Mon, 11 Jun 2018 11:29:04 +0000 (11:29 +0000)
Valgrind reported memory leak with below backtrace
==3471== 1,934 (80 direct, 1,854 indirect) bytes in 2 blocks are
definitely lost in loss record 190 of 194
==3471==    at 0x484801C: calloc (vg_replace_malloc.c:711)
==3471==    by 0x49D687B: dbus_malloc0 (dbus-memory.c:572)
==3471==    by 0x49BB9A3: _dbus_pending_call_new_unlocked (dbus-pending-call.c:124)
==3471==    by 0x4995963: dbus_connection_send_with_reply (dbus-connection.c:3449)
==3471==    by 0x135D8D: __dbus_request.constprop.8 (telephony.c:396)
==3471==    by 0x135E6D: __request_get_services (telephony.c:477)
==3471==    by 0x136F19: __add_modem (telephony.c:802)
==3471==    by 0x13732F: __response_get_modems (telephony.c:464)
==3471==    by 0x498FABB: complete_pending_call_and_unlock (dbus-connection.c:2340)
==3471==    by 0x49981BF: dbus_connection_dispatch (dbus-connection.c:4757)
==3471==    by 0x18E815: message_dispatch (mainloop.c:72)
==3471==    by 0x48BB8E7: g_main_dispatch (gmain.c:3234)
==3471==    by 0x48BB8E7: g_main_context_dispatch (gmain.c:3887

When dbus response is received then we should unref memory allocated by
libdbus

Change-Id: I81c514506f0df42e537d63382b2eb2ebc51c58c8
Signed-off-by: Saurav Babu <saurav.babu@samsung.com>
plugins/telephony.c

index 2b4c5a4..2954c18 100755 (executable)
@@ -390,8 +390,10 @@ static int __dbus_request(const char *path, const char *interface,
        ok = dbus_message_append_args_valist(message, type, va);
        va_end(va);
 
-       if (!ok)
+       if (!ok) {
+               dbus_message_unref(message);
                return -ENOMEM;
+       }
 
        if (dbus_connection_send_with_reply(connection, message,
                                                &call, TIMEOUT) == FALSE) {
@@ -489,10 +491,6 @@ static void __response_get_services(DBusPendingCall *call, void *user_data)
        struct telephony_modem *modem;
 
        modem = g_hash_table_lookup(modem_hash, path);
-       if (modem == NULL)
-               return;
-       if (modem->device == NULL)
-               return;
 
        DBG("");
 
@@ -506,6 +504,9 @@ static void __response_get_services(DBusPendingCall *call, void *user_data)
                goto done;
        }
 
+       if (modem == NULL || modem->device == NULL)
+               goto done;
+
        DBG("message signature (%s)", dbus_message_get_signature(reply));
 
        if (dbus_message_iter_init(reply, &args) == FALSE)