From b95147f73df415473f40fdc5cc32c85dbbc02ef3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sat, 24 Apr 2010 12:49:16 +0000 Subject: [PATCH] * DeleteEntryFromHash(): don't skip indirection via the lookuptable, else we free some random payload * DeleteEntryFromHash(): NULL the end of the list; This is the entry we just removed. * hashlist_test: add some tests with removing items from the list --- libcitadel/lib/hash.c | 5 ++- libcitadel/tests/hashlist_test.c | 63 +++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/libcitadel/lib/hash.c b/libcitadel/lib/hash.c index f9ceeced3..ac6a377ff 100644 --- a/libcitadel/lib/hash.c +++ b/libcitadel/lib/hash.c @@ -651,8 +651,8 @@ int DeleteEntryFromHash(HashList *Hash, HashPos *At) return 0; } - FreeMe = Hash->Members[At->Position]; - Hash->Members[At->Position] = NULL; + FreeMe = Hash->Members[Hash->LookupTable[At->Position]->Position]; + Hash->Members[Hash->LookupTable[At->Position]->Position] = NULL; /** delete our hashing data */ @@ -667,6 +667,7 @@ int DeleteEntryFromHash(HashList *Hash, HashPos *At) (Hash->nLookupTableItems - At->Position - 1) * sizeof(HashKey*)); + Hash->LookupTable[Hash->nLookupTableItems - 1] = NULL; } else Hash->LookupTable[At->Position] = NULL; diff --git a/libcitadel/tests/hashlist_test.c b/libcitadel/tests/hashlist_test.c index 7c7a9ad6e..fc638ccbc 100644 --- a/libcitadel/tests/hashlist_test.c +++ b/libcitadel/tests/hashlist_test.c @@ -66,12 +66,8 @@ static void test_iterate_hash(HashList *testh, int forward, int stepwidth) DeleteHashPos(&it); } -static void TestHashlistIteratorForward (void) +static void ValidateBackAndForth(HashList *H) { - HashList *H; - - H = GetFilledHash (10, 1); - test_iterate_hash(H, 1, 1); printf("\n"); @@ -89,9 +85,64 @@ static void TestHashlistIteratorForward (void) test_iterate_hash(H, 0, 3); printf("\n"); +} + +static void TestHashlistIteratorForward (void) +{ + HashList *H; + + H = GetFilledHash (10, 1); + + ValidateBackAndForth(H); + + DeleteHash(&H); +} + + +static void TestHashlistAddDelete (void) +{ + HashList *H; + HashPos *at; + int *val, i; + + H = GetFilledHash (10, 1); + + at = GetNewHashPos(H, 0); + + printf("Remove first\n"); + DeleteEntryFromHash(H, at); + + ValidateBackAndForth(H); + + printf("Insert 15\n"); + i = 15; + val = (int*) malloc(sizeof(int)); + *val = i; + Put(H, IKEY(i), val, NULL); + + ValidateBackAndForth(H); + + printf("Remove third\n"); + at = GetNewHashPos(H, 0); + NextHashPos(H, at); + NextHashPos(H, at); + NextHashPos(H, at); + DeleteEntryFromHash(H, at); + + ValidateBackAndForth(H); + printf("Insert -15\n"); + + i = -15; + val = (int*) malloc(sizeof(int)); + *val = i; + Put(H, IKEY(i), val, NULL); + + ValidateBackAndForth(H); + DeleteHash(&H); } + /* Some samples from the original... CU_ASSERT_EQUAL(10, 10); @@ -133,6 +184,8 @@ static void AddHashlistTests(void) pGroup = CU_add_suite("TestStringBufSimpleAppenders", NULL, NULL); pTest = CU_add_test(pGroup, "TestHashListIteratorForward", TestHashlistIteratorForward); + pTest = CU_add_test(pGroup, "TestHashlistAddDelete", TestHashlistAddDelete); + } -- 2.30.2