]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
list: LIST_INSERT_BEFORE: update head if necessary (#4261)
authorMichael Olbrich <m.olbrich@pengutronix.de>
Tue, 4 Oct 2016 14:15:37 +0000 (16:15 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 4 Oct 2016 14:15:37 +0000 (16:15 +0200)
If the new item is inserted before the first item in the list, then the
head must be updated as well.
Add a test to the list unit test to check for this.

src/basic/list.h
src/test/test-list.c

index 5962aa42113b5d425cd181241a7f17da9ef43439..c3771a177f70e494b94d868a62056bc5d8f6e6be 100644 (file)
                 } else {                                                \
                         if ((_b->name##_prev = _a->name##_prev))        \
                                 _b->name##_prev->name##_next = _b;      \
+                        else                                            \
+                                *_head = _b;                            \
                         _b->name##_next = _a;                           \
                         _a->name##_prev = _b;                           \
                 }                                                       \
index 160064d06a9992cc6a28bb20b013001faf075d92..0ccd745cc913bf7d21161b2da9a5f7aa9fc9130b 100644 (file)
@@ -132,6 +132,29 @@ int main(int argc, const char *argv[]) {
         assert_se(items[1].item_prev == &items[3]);
         assert_se(items[3].item_prev == NULL);
 
+        LIST_INSERT_BEFORE(item, head, &items[3], &items[0]);
+        assert_se(items[2].item_next == NULL);
+        assert_se(items[1].item_next == &items[2]);
+        assert_se(items[3].item_next == &items[1]);
+        assert_se(items[0].item_next == &items[3]);
+
+        assert_se(items[2].item_prev == &items[1]);
+        assert_se(items[1].item_prev == &items[3]);
+        assert_se(items[3].item_prev == &items[0]);
+        assert_se(items[0].item_prev == NULL);
+        assert_se(head == &items[0]);
+
+        LIST_REMOVE(item, head, &items[0]);
+        assert_se(LIST_JUST_US(item, &items[0]));
+
+        assert_se(items[2].item_next == NULL);
+        assert_se(items[1].item_next == &items[2]);
+        assert_se(items[3].item_next == &items[1]);
+
+        assert_se(items[2].item_prev == &items[1]);
+        assert_se(items[1].item_prev == &items[3]);
+        assert_se(items[3].item_prev == NULL);
+
         LIST_INSERT_BEFORE(item, head, NULL, &items[0]);
         assert_se(items[0].item_next == NULL);
         assert_se(items[2].item_next == &items[0]);