Fix memory leak of return value of afb_req_get_application_id
[apps/agl-service-windowmanager-2017.git] / src / main.cpp
index b0c9415..964abde 100644 (file)
@@ -18,6 +18,7 @@
 #include <algorithm>
 #include <mutex>
 #include <json.h>
+#include <stdlib.h>
 #include "window_manager.hpp"
 #include "json_helper.hpp"
 
@@ -143,18 +144,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)
     {
@@ -192,19 +201,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;
+            auto ret = g_afb_instance->wmgr.api_request_surface(
+                appid, a_drawing_name, a_ivi_id);
+
+            if (ret != nullptr)
+            {
+                afb_req_fail(req, "failed", ret);
+            }
+            else
+            {
+                createSecurityContext(req, appid, a_drawing_name);
+                afb_req_success(req, NULL, "success");
+            }
+            free(appid);
         }
-
-        createSecurityContext(req, appid, a_drawing_name);
-
-        afb_req_success(req, NULL, "success");
     }
     catch (std::exception &e)
     {
@@ -223,7 +236,6 @@ void windowmanager_setrole(afb_req req) noexcept
     }
     try
     {
-        char const *appid = afb_req_get_application_id(req);
         json_object *jreq = afb_req_json(req);
 
         json_object *j_role = nullptr;
@@ -233,17 +245,22 @@ void windowmanager_setrole(afb_req req) noexcept
             return;
         }
         char const *a_role = json_object_get_string(j_role);
+        char *appid = afb_req_get_application_id(req);
 
-        auto ret = g_afb_instance->wmgr.api_set_role(appid, a_role);
-        if (!ret)
+        if(appid)
         {
-            afb_req_fail(req, "failed", "Couldn't register");
-            return;
+            auto ret = g_afb_instance->wmgr.api_set_role(appid, a_role);
+            if (!ret)
+            {
+                afb_req_fail(req, "failed", "Couldn't register");
+            }
+            else
+            {
+                createSecurityContext(req, appid, a_role);
+                afb_req_success(req, NULL, "success");
+            }
+            free(appid);
         }
-
-        createSecurityContext(req, appid, a_role);
-
-        afb_req_success(req, NULL, "success");
     }
     catch (std::exception &e)
     {
@@ -278,18 +295,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(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(errmsg);
+                        afb_req_fail(req, "failed", errmsg);
+                        return;
+                    }
+                    afb_req_success(req, NULL, "success");
+                });
+            free(appid);
+        }
     }
     catch (std::exception &e)
     {
@@ -318,17 +339,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(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(errmsg);
+                        afb_req_fail(req, "failed", errmsg);
+                        return;
+                    }
+                    afb_req_success(req, NULL, "success");
+                });
+            free(appid);
+        }
     }
     catch (std::exception &e)
     {
@@ -358,8 +384,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)
     {