Browse Source

insque: fix segfault on (prev == NULL)

Since version 2.5 glibc allows prev to be a NULL pointer in insque, whereas
uClibc segfaults in this case. This fixes the issue and makes insque
initialize q_forw and q_back with NULLs if prev == NULL.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Bartosz Golaszewski 11 years ago
parent
commit
f9ae36ebf1
1 changed files with 14 additions and 6 deletions
  1. 14 6
      libc/misc/search/insremque.c

+ 14 - 6
libc/misc/search/insremque.c

@@ -26,12 +26,20 @@
 void
 insque (void *elem, void *prev)
 {
-  struct qelem *next = ((struct qelem *) prev)->q_forw;
-  ((struct qelem *) prev)->q_forw = (struct qelem *) elem;
-  if (next != NULL)
-    next->q_back = (struct qelem *) elem;
-  ((struct qelem *) elem)->q_forw = next;
-  ((struct qelem *) elem)->q_back = (struct qelem *) prev;
+  if (prev == NULL)
+    {
+      ((struct qelem *) elem)->q_forw = NULL;
+      ((struct qelem *) elem)->q_back = NULL;
+    }
+  else
+    {
+      struct qelem *next = ((struct qelem *) prev)->q_forw;
+      ((struct qelem *) prev)->q_forw = (struct qelem *) elem;
+      if (next != NULL)
+        next->q_back = (struct qelem *) elem;
+      ((struct qelem *) elem)->q_forw = next;
+      ((struct qelem *) elem)->q_back = (struct qelem *) prev;
+    }
 }
 
 #endif