diff --git a/src/util/attrib.c b/src/util/attrib.c index 26869cfee..5c92b691e 100644 --- a/src/util/attrib.c +++ b/src/util/attrib.c @@ -199,11 +199,17 @@ static void a_free(attrib * a) int a_remove(attrib ** pa, attrib * a) { + attrib *head = *pa; int ok; + assert(a != NULL); ok = a_unlink(pa, a); - if (ok) + if (ok) { + if (head == a) { + *pa = a->next; + } a_free(a); + } return ok; } diff --git a/src/util/attrib.test.c b/src/util/attrib.test.c index 479809d1d..691087477 100644 --- a/src/util/attrib.test.c +++ b/src/util/attrib.test.c @@ -39,6 +39,18 @@ static void test_attrib_add(CuTest * tc) a_removeall(&alist, &at_bar); } +static void test_attrib_remove_self(CuTest * tc) { + attrib_type at_foo = { "foo" }; + attrib *a, *alist = 0; + + CuAssertPtrNotNull(tc, a_add(&alist, a_new(&at_foo))); + CuAssertPtrNotNull(tc, a = a_add(&alist, a_new(&at_foo))); + CuAssertPtrEquals(tc, a, alist->next); + CuAssertPtrEquals(tc, 0, alist->nexttype); + CuAssertIntEquals(tc, 1, a_remove(&alist, alist)); + CuAssertPtrEquals(tc, a, alist); +} + static void test_attrib_remove(CuTest * tc) { attrib_type at_foo = { "foo" }; @@ -86,6 +98,7 @@ CuSuite *get_attrib_suite(void) SUITE_ADD_TEST(suite, test_attrib_new); SUITE_ADD_TEST(suite, test_attrib_add); SUITE_ADD_TEST(suite, test_attrib_remove); + SUITE_ADD_TEST(suite, test_attrib_remove_self); SUITE_ADD_TEST(suite, test_attrib_nexttype); return suite; }