Fix inconsistent string ownership 12/185812/2
authorMichal Bloch <m.bloch@samsung.com>
Thu, 2 Aug 2018 11:42:44 +0000 (13:42 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 2 Aug 2018 12:42:48 +0000 (14:42 +0200)
Usually the PPI structure owns its own strdup'd copy of the pkgname string.
However, sometimes it shared it in a non-owning way with another structure
which risked the possibility of trying to use it after that other structure
had already been destroyed.

Change-Id: I71433d24137ef8e96325e9a2aeece53b51b079b5
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/proc-stat/proc-main.c

index 1a27b6b3aa7b5c0b92e37ebfbc8c2fe9e9df726b..5a770febcc21fdca4e65f6f5c53580066883efe6 100644 (file)
@@ -532,14 +532,14 @@ static struct proc_program_info *proc_add_program_list(const int type,
                }
 
                if (pai->ai)
-                       ppi->pkgname = pai->ai->pkgname;
-               else {
+                       ppi->pkgname = strdup(pai->ai->pkgname);
+               else
                        ppi->pkgname = strndup(pkgname, strlen(pkgname)+1);
-                       if (!ppi->pkgname) {
-                               _E("Fail to add %s : not enough memory", pkgname);
-                               free(ppi);
-                               return NULL;
-                       }
+
+               if (!ppi->pkgname) {
+                       _E("Fail to add %s : not enough memory", pkgname);
+                       free(ppi);
+                       return NULL;
                }
                proc_program_list = g_slist_prepend(proc_program_list, ppi);
                _I("%s is added in the program info", pkgname);