OSDN Git Service

Another try at fixing the ps segfault resulting from /proc entries vanishing
authorRob Landley <rob@landley.net>
Thu, 31 Aug 2017 21:50:27 +0000 (16:50 -0500)
committerRob Landley <rob@landley.net>
Thu, 31 Aug 2017 21:50:27 +0000 (16:50 -0500)
out from under us due to asyncronous process exit.

The directory we're traversing vanishing can result in DIRTREE_ABORTVAL
being returned, which we turn into a NUL entry, but then we were trying to
look at that null entry's children. Oops.

toys/posix/ps.c

index a0dc53f..aef2a7f 100644 (file)
@@ -915,15 +915,16 @@ static int get_threads(struct dirtree *new)
   // Save or display
   if (!TT.show_process) return DIRTREE_SAVE;
   TT.show_process((void *)new->extra);
-  dt = new->child;
-  new->child = 0;
-  while (dt->child) {
-    new = dt->child->next;
-    TT.show_process((void *)dt->child->extra);
-    free(dt->child);
-    dt->child = new;
+  if ((dt = new->child)) {
+    new->child = 0;
+    while (dt->child) {
+      new = dt->child->next;
+      TT.show_process((void *)dt->child->extra);
+      free(dt->child);
+      dt->child = new;
+    }
+    free(dt);
   }
-  free(dt);
 
   return 0;
 }