From 8bc2c9fc0fa46f4ee092138f0279f4044f69f526 Mon Sep 17 00:00:00 2001 From: garbeam Date: Wed, 7 Dec 2005 18:58:04 +0200 Subject: [PATCH] fixed error in container.c, but still a weird bug on move --- libcext/container.c | 11 ++++++++--- test/test_container.c | 13 +++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) 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");