diff --git a/libcext/container.c b/libcext/container.c index d4caa217..9489c8fa 100644 --- a/libcext/container.c +++ b/libcext/container.c @@ -20,7 +20,8 @@ static void detach_from_stack(Container *c, CItem *i) /* remove from stack */ if (i == c->stack) { c->stack = i->down; - c->stack->up = 0; + if (c->stack) + c->stack->up = 0; } else { if (i->up) @@ -64,14 +65,18 @@ void cext_detach_item(Container *c, void *item) CItem *i = c->list; if (!i) return; + /* remove from list */ if (i->item == item) - c->list = c->list->next; + c->list = i->next; else { + CItem *tmp; for (; i->next && (i->next->item != item); i = i->next); if (!i->next) return; - i->next = i->next->next; + tmp = i; + i = i->next; + tmp->next = tmp->next->next; } detach_from_stack(c, i); free(i); diff --git a/test/test_container.c b/test/test_container.c index 7e6d1ea7..aae2750d 100644 --- a/test/test_container.c +++ b/test/test_container.c @@ -19,13 +19,22 @@ int main(int argc, char *argv[]) Container c = {0}; int i; + printf("--------------------------------\n"); + { + int *e = cext_emalloc(sizeof(int)); + cext_attach_item(&c, e); + cext_iterate(&c, nil, iter_print_container); + cext_detach_item(&c, e); + cext_iterate(&c, nil, iter_print_container); + } + printf("--------------------------------\n"); + + printf("--------------------------------\n"); for (i = 0; i < 10; i++) { int *e = cext_emalloc(sizeof(int)); *e = i; cext_attach_item(&c, e); } - - printf("--------------------------------\n"); cext_iterate(&c, nil, iter_print_container); printf("--------------------------------\n");