From 6a20800629fca350b00ce71a09ba4d71e2f2ee1d Mon Sep 17 00:00:00 2001
From: Kazumasa Mitsunari <knimitz@witz-inc.co.jp>
Date: Mon, 15 Oct 2018 19:58:51 +0900
Subject: Fix memory leak

According to afb_x2_req.h,
returned value of afb_req_x2_get_application_id must be freed by the caller.
So release it when the function returned not Null.

cherry-picked from master

Bug-AGL: SPEC-1819

Change-Id: I4f23eeff6262171ac55776b4e214960dbff7ff45
Signed-off-by: Kazumasa Mitsunari <knimitz@witz-inc.co.jp>
---
 src/main.cpp | 117 ++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 71 insertions(+), 46 deletions(-)

(limited to 'src')

diff --git a/src/main.cpp b/src/main.cpp
index 0447f86..e6cccb5 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -236,18 +236,26 @@ void windowmanager_requestsurface(afb_req req) noexcept
             return;
         }
 
-        const char *appid = afb_req_get_application_id(req);
-        auto ret = g_afb_instance->wmgr.api_request_surface(
-            appid, a_drawing_name);
-        if (ret.is_err())
+        char *appid = afb_req_get_application_id(req);
+        if(appid)
         {
-            afb_req_fail(req, "failed", ret.unwrap_err());
-            return;
+            auto ret = g_afb_instance->wmgr.api_request_surface(
+                appid, a_drawing_name);
+            if (ret.is_err())
+            {
+                afb_req_fail(req, "failed", ret.unwrap_err());
+            }
+            else
+            {
+                createSecurityContext(req, appid, a_drawing_name);
+                afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
+            }
+            free(appid);
+        }
+        else
+        {
+            afb_req_fail(req, "failed", nullptr);
         }
-
-        createSecurityContext(req, appid, a_drawing_name);
-
-        afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
     }
     catch (std::exception &e)
     {
@@ -287,19 +295,23 @@ void windowmanager_requestsurfacexdg(afb_req req) noexcept
             return;
         }
         char const *a_ivi_id = json_object_get_string(j_ivi_id);
-        char const *appid = afb_req_get_application_id(req);
-        auto ret = g_afb_instance->wmgr.api_request_surface(
-            appid, a_drawing_name, a_ivi_id);
-
-        if (ret != nullptr)
+        char *appid = afb_req_get_application_id(req);
+        if(appid)
         {
-            afb_req_fail(req, "failed", ret);
-            return;
-        }
-
-        createSecurityContext(req, appid, a_drawing_name);
+            auto ret = g_afb_instance->wmgr.api_request_surface(
+                appid, a_drawing_name, a_ivi_id);
 
-        afb_req_success(req, NULL, "success");
+            if (ret != nullptr)
+            {
+                afb_req_fail(req, "failed", ret);
+            }
+            else
+            {
+                createSecurityContext(req, appid, a_drawing_name);
+                afb_req_success(req, NULL, "success");
+            }
+            free(appid);
+        }
     }
     catch (std::exception &e)
     {
@@ -336,18 +348,22 @@ void windowmanager_activatewindow(afb_req req) noexcept
             return;
         }
 
-        g_afb_instance->wmgr.api_activate_surface(
-            afb_req_get_application_id(req),
-            a_drawing_name, a_drawing_area,
-            [&req](const char *errmsg) {
-                if (errmsg != nullptr)
-                {
-                    HMI_ERROR("wm", errmsg);
-                    afb_req_fail(req, "failed", errmsg);
-                    return;
-                }
-                afb_req_success(req, NULL, "success");
-            });
+        char* appid = afb_req_get_application_id(req);
+        if(appid)
+        {
+            g_afb_instance->wmgr.api_activate_surface(
+                appid, a_drawing_name, a_drawing_area,
+                [&req](const char *errmsg) {
+                    if (errmsg != nullptr)
+                    {
+                        HMI_ERROR("wm", errmsg);
+                        afb_req_fail(req, "failed", errmsg);
+                        return;
+                    }
+                    afb_req_success(req, NULL, "success");
+                });
+            free(appid);
+        }
     }
     catch (std::exception &e)
     {
@@ -378,17 +394,22 @@ void windowmanager_deactivatewindow(afb_req req) noexcept
             return;
         }
 
-        g_afb_instance->wmgr.api_deactivate_surface(
-            afb_req_get_application_id(req), a_drawing_name,
-            [&req](const char *errmsg) {
-                if (errmsg != nullptr)
-                {
-                    HMI_ERROR("wm", errmsg);
-                    afb_req_fail(req, "failed", errmsg);
-                    return;
-                }
-                afb_req_success(req, NULL, "success");
-            });
+        char* appid = afb_req_get_application_id(req);
+        if(appid)
+        {
+            g_afb_instance->wmgr.api_deactivate_surface(
+                appid, a_drawing_name,
+                [&req](const char *errmsg) {
+                    if (errmsg != nullptr)
+                    {
+                        HMI_ERROR("wm", errmsg);
+                        afb_req_fail(req, "failed", errmsg);
+                        return;
+                    }
+                    afb_req_success(req, NULL, "success");
+                });
+            free(appid);
+        }
     }
     catch (std::exception &e)
     {
@@ -420,8 +441,12 @@ void windowmanager_enddraw(afb_req req) noexcept
         }
         afb_req_success(req, NULL, "success");
 
-        g_afb_instance->wmgr.api_enddraw(
-            afb_req_get_application_id(req), a_drawing_name);
+        char* appid = afb_req_get_application_id(req);
+        if(appid)
+        {
+            g_afb_instance->wmgr.api_enddraw(appid, a_drawing_name);
+            free(appid);
+        }
     }
     catch (std::exception &e)
     {
-- 
cgit