Fix dangling pointer issue with command list 67/135267/1
authorWonnam Jang <wn.jang@samsung.com>
Wed, 21 Jun 2017 11:12:11 +0000 (20:12 +0900)
committerWonnam Jang <wn.jang@samsung.com>
Wed, 21 Jun 2017 11:12:11 +0000 (20:12 +0900)
Change-Id: I028c2f9972c587b2143d1dc0e225eb80dd1f5af5
Signed-off-by: Wonnam Jang <wn.jang@samsung.com>
common/vc_command.c

index 1478fcd..5ae5859 100644 (file)
@@ -37,6 +37,8 @@ static int g_feature_enabled = -1;
 
 static int g_privilege_allowed = 1; /* Always True */
 static cynara *p_cynara = NULL;
+static GList *g_cmdlist_list = NULL;
+
 
 // For getting timestamp using regular expression
 static regex_t reg[MAX_NUM_REGEX];
@@ -178,6 +180,8 @@ int vc_cmd_list_create(vc_cmd_list_h* vc_cmd_list)
 
        *vc_cmd_list = (vc_cmd_list_h)list;
 
+       g_cmdlist_list = g_list_append(g_cmdlist_list, list);
+
        SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p)", *vc_cmd_list);
 
        return VC_ERROR_NONE;
@@ -204,12 +208,34 @@ int vc_cmd_list_destroy(vc_cmd_list_h vc_cmd_list, bool release_command)
 
        SLOG(LOG_DEBUG, TAG_VCCMD, "[List] list(%p)", list);
 
-       if (NULL != list) {
-               free(list);
-               list = NULL;
+       GList *iter = NULL;
+       vc_cmd_list_s *data = NULL;
+
+       /* if list have item */
+       if (g_list_length(g_cmdlist_list) > 0) {
+               /* Get a first item */
+               iter = g_list_first(g_cmdlist_list);
+
+               while (NULL != iter) {
+                       data = iter->data;
+                       if (data && list == data) {
+                               g_cmdlist_list = g_list_remove_link(g_cmdlist_list, iter);
+
+                               free(data);
+                               data = NULL;
+
+                               SLOG(LOG_DEBUG, TAG_VCCMD, "Client destroy");
+                               g_list_free(iter);
+
+                               return VC_ERROR_NONE;
+                       }
+                       /* Next item */
+                       iter = g_list_next(iter);
+               }
        }
+       SLOG(LOG_ERROR, TAG_VCCMD, "Fail to destroy client : handle is not valid");
 
-       return VC_ERROR_NONE;
+       return VC_ERROR_INVALID_PARAMETER;
 }
 
 int vc_cmd_list_get_count(vc_cmd_list_h vc_cmd_list, int* count)