Fix memory corruption issue when unregistering surfaces 11/19411/3
authorNick Diego Yamane <nickdiego@igalia.com>
Wed, 19 Dec 2018 07:00:30 +0000 (03:00 -0400)
committerJan-Simon Moeller <jsmoeller@linuxfoundation.org>
Tue, 15 Jan 2019 11:18:39 +0000 (11:18 +0000)
- Iterator was being incremented twice, one when erase()
  is called (implicit [1]) and another one just after to call erase
  (explicitly), when unregistering a pid/surface_id.

Bug-AGL: SPEC-2078

[1] https://www.techiedelight.com/remove-elements-vector-inside-loop-cpp

Change-Id: Ia3cc3981480cf76b839043be49d257d5be011d60
Signed-off-by: Nick Diego Yamane <nickdiego@igalia.com>
src/runxdg.cpp

index 3ff942c..f47c6aa 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/time.h>
 #include <sys/wait.h>
 
+#include <algorithm>
 #include <cstdio>
 
 #include "cpptoml/cpptoml.h"
@@ -513,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)