Fix memory leak caused by improper handling of GList 20/208120/2
authorTomasz Swierczek <t.swierczek@samsung.com>
Tue, 18 Jun 2019 10:56:02 +0000 (12:56 +0200)
committerTomasz Swierczek <t.swierczek@samsung.com>
Wed, 19 Jun 2019 10:23:26 +0000 (12:23 +0200)
New API from privilege-checker was used.

Change-Id: I77913b31e6d9b0e259dfae38d7c9a5c7ef0c7587

src/common/policy/PrivilegeInfo.cpp

index 882ac12..6ebf193 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2016-2018 Samsung Electronics Co.
+ *  Copyright (c) 2016-2019 Samsung Electronics Co.
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
  */
 
 #include <cstdlib>
+#include <functional>
 #include <memory>
 #include <set>
 #include <sys/types.h>
@@ -43,7 +44,7 @@ namespace {
 
 class GListWrap {
 public:
-    GListWrap() : m_head(nullptr) {
+    GListWrap() : m_head(nullptr), m_deleter(g_list_free) {
         m_head = g_list_alloc();
         if (!m_head) {
             ALOGE("Failed to allocate glib list");
@@ -51,9 +52,10 @@ public:
         }
     }
 
-    GListWrap(GList *_list) : m_head(_list) {}
+    GListWrap(GList *_list, std::function<void(GList*)> deleter = g_list_free)
+            : m_head(_list), m_deleter(deleter) {}
 
-    ~GListWrap() { g_list_free(m_head);}
+    ~GListWrap() { m_deleter(m_head);}
 
     void append(gpointer data) {
         GList *newHead = g_list_prepend(m_head, data);
@@ -67,6 +69,7 @@ public:
 
 private:
     GList *m_head;
+    std::function<void(GList*)> m_deleter;
 };
 
 Privacy getPrivacyName(const Privilege &privilege) {
@@ -136,7 +139,7 @@ std::vector<Privilege> getCorePrivilegeMapping(AppInfo &appInfo, const std::stri
         return {};
     }
 
-    GListWrap privMappedWrap(privMapped);
+    GListWrap privMappedWrap(privMapped, privilege_db_manager_list_free);
     std::vector<std::string> privMappedVector;
     for (GList *l = privMappedWrap.get(); l != NULL; l = l->next) {
         std::string corePriv = static_cast<char*>(l->data);
@@ -159,7 +162,7 @@ std::vector<Privilege> getSamePrivacyPrivilegeMapping(const Privilege &privilege
         return {};
     }
 
-    GListWrap privMappedWrap(privMapped);
+    GListWrap privMappedWrap(privMapped, privilege_info_list_free);
     std::vector<std::string> privMappedVector;
     for (GList *l = privMappedWrap.get(); l != NULL; l = l->next) {
         std::string corePriv = static_cast<char*>(l->data);
@@ -194,7 +197,7 @@ std::vector<Privilege> getPrivacyPrivileges(const Privacy &privacy) {
         return {privacy};
     }
 
-    GListWrap privList(privilegeList);
+    GListWrap privList(privilegeList, privilege_info_list_free);
     std::vector<Privilege> privVector;
     for (GList *l = privList.get(); l != NULL; l = l->next) {
         privVector.push_back(static_cast<char*>(l->data));