Fix memory corruption issue when unregistering surfaces
[staging/xdg-launcher.git] / src / runxdg.cpp
index 5553ff5..f47c6aa 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/time.h>
 #include <sys/wait.h>
 
+#include <algorithm>
 #include <cstdio>
 
 #include "cpptoml/cpptoml.h"
@@ -410,7 +411,29 @@ int RunXDG::parse_config (const char *path_to_config)
   auto params = app->get_array_of<std::string>("params");
   for (const auto& param : *params)
   {
+    // replace special string "@port@" and "@token@"
+    size_t found = param.find("@port@");
+    if (found != std::string::npos) {
+      std::string sub1 = param.substr(0, found);
+      std::string sub2 = param.substr(found + 6, param.size() - found);
+      std::string str = sub1 + std::to_string(m_port) + sub2;
+      pl->m_args_v.push_back(str);
+      AGL_DEBUG("params[%s] (match @port@)", str.c_str());
+      continue;
+    }
+
+    found = param.find("@token@");
+    if (found != std::string::npos) {
+      std::string sub1 = param.substr(0, found);
+      std::string sub2 = param.substr(found + 7, param.size() - found);
+      std::string str = sub1 + m_token + sub2;
+      pl->m_args_v.push_back(str);
+      AGL_DEBUG("params[%s] (match @token@)", str.c_str());
+      continue;
+    }
+
     pl->m_args_v.push_back(param);
+
     AGL_DEBUG("params[%s]", param.c_str());
   }
 
@@ -491,14 +514,10 @@ void POSIXLauncher::register_surfpid (pid_t surf_pid)
 
 void POSIXLauncher::unregister_surfpid (pid_t surf_pid)
 {
-  auto itr = m_pid_v.begin();
-  while (itr != m_pid_v.end()) {
-    if (*itr == surf_pid) {
-      m_pid_v.erase(itr++);
-    } else {
-      ++itr;
-    }
-  }
+  auto beg = m_pid_v.begin();
+  auto end = m_pid_v.end();
+  m_pid_v.erase(std::remove(beg, end, surf_pid), end);
+  AGL_DEBUG("Unregistered surface (id=%d sz=%u)", surf_pid, m_pid_v.size());
 }
 
 pid_t POSIXLauncher::find_surfpid_by_rid (pid_t rid)