Memory leaks in popup runner
authorPrzemyslaw Ciezkowski <p.ciezkowski@samsung.com>
Fri, 12 Oct 2012 13:40:04 +0000 (15:40 +0200)
committerJihoon Chung <jihoon.chung@samsung.com>
Fri, 2 Nov 2012 07:06:31 +0000 (16:06 +0900)
[Issue#] LINUXNGWAP-442
[Problem] Memory leaks in wrt-popup-run and unhandled exception
from singleton Impl.
[Cause] N/A
[Solution] Fix memory leaks and add exception handling.
[SCMRequest] N/A
[Verification] Change policy to prompt-session, run any WAC
widget. Popup will appear asking for permission. Check if
it works and ends normally.

Change-Id: Ia51b118ca94af7e6a465212893eb44e61a1626d1

src/wrt-popup/popup-bin/Popup.cpp

index cbeec8d..9799f51 100644 (file)
@@ -143,17 +143,34 @@ static int _ace_params_deserializer(ace_param_list_t* ace_param_list, Wrt::Popup
         return 0;
     }
     ace_param_list->items = static_cast <ace_param_t *> (malloc(count * sizeof(ace_param_t)));
+    memset(ace_param_list->items, 0, count * sizeof(ace_param_t));
 
     for(size_t i=0; i < count; ++i){
         DPL::Deserialization::Deserialize(*stream, name);
-        ace_param_list->items[i].name = const_cast <char *> (name.c_str());
+        ace_param_list->items[i].name =
+                strdup(const_cast <char *> (name.c_str()));
 
         DPL::Deserialization::Deserialize(*stream, value);
-        ace_param_list->items[i].value = const_cast <char *> (value.c_str());
+        ace_param_list->items[i].value =
+                strdup(const_cast <char *> (value.c_str()));
     }
     return 0;
 }
 
+static void _ace_params_finalize(ace_param_list_t* ace_param_list) {
+    if (NULL == ace_param_list || ace_param_list->items) {
+        LogDebug("List is null, nothing to do");
+        return;
+    }
+    for (size_t i = 0; i < ace_param_list->count; ++i) {
+        free(ace_param_list->items[i].name);
+        free(ace_param_list->items[i].value);
+    }
+    free(ace_param_list->items);
+    ace_param_list->items = NULL;
+    ace_param_list->count = 0;
+}
+
 static void show_popup(struct ace_popup_data *pdp) {
     LogDebug("show_popup()");
 
@@ -265,8 +282,12 @@ elm_main(int argc , char ** argv)
 //  ace_widget_handle_t     handle
 //  const ace_param_list_t  param_list
 
-
-    DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-POPUP-BIN");
+    try {
+        DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-POPUP-BIN");
+    } Catch(DPL::Exception) {
+        //cannot run logger
+        return ACE_INTERNAL_ERROR;
+    }
     LogDebug("############################ popup binary ################################");
 
     if(argc < 3){
@@ -342,11 +363,13 @@ elm_main(int argc , char ** argv)
 
     DPL::Deserialization::Deserialize(stream, resource_name_str);
     LogDebug("resource_name_char : " << resource_name_str.c_str());
-    pdp->resource_name = const_cast <ace_resource_t> (resource_name_str.c_str());
+    pdp->resource_name =
+            strdup(const_cast <ace_resource_t> (resource_name_str.c_str()));
 
     DPL::Deserialization::Deserialize(stream, session_id_str);
     LogDebug("session_id_char : " << session_id_str.c_str());
-    pdp->session_id = const_cast <ace_session_id_t> (session_id_str.c_str());
+    pdp->session_id =
+            strdup(const_cast <ace_session_id_t> (session_id_str.c_str()));
 
     DPL::Deserialization::Deserialize(stream, handle);
     LogDebug("handle_int : " << handle);
@@ -401,6 +424,7 @@ elm_main(int argc , char ** argv)
     // Not calling elm_shutdown() should not have any negative consequences because this binary ends
     // in next line, and system should clear the memory.
 
+    _ace_params_finalize(&(pdp->param_list));
     return pdp->validation_return;
 }
 ELM_MAIN()