Skip to content

Commit

Permalink
ptrie: deref the current node in trie_iter_free()
Browse files Browse the repository at this point in the history
If free'ing the iterator before getting to the last
node make sure we de-ref the current node. Else we
will not be able to delete the node.

fixes #44

Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
  • Loading branch information
asalkeld committed Dec 6, 2012
1 parent 30a7871 commit 813dfb5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/trie.c
Expand Up @@ -757,6 +757,15 @@ trie_iter_next(qb_map_iter_t * i, void **value)
static void
trie_iter_free(qb_map_iter_t * i)
{
struct trie_iter *si = (struct trie_iter *)i;
struct trie *t = (struct trie *)(i->m);

if (si->n != NULL) {
/* if free'ing the iterator before getting to the last
* node make sure we de-ref the current node.
*/
trie_node_deref(t, si->n);
}
free(i);
}

Expand Down
38 changes: 38 additions & 0 deletions tests/check_map.c
Expand Up @@ -870,6 +870,40 @@ START_TEST(test_trie_load)
}
END_TEST

/*
* From Honza: https://github.com/asalkeld/libqb/issues/44
*/
START_TEST(test_trie_partial_iterate)
{
qb_map_t *map;
qb_map_iter_t *iter;
const char *res;
char *item;
int rc;

ck_assert((map = qb_trie_create()) != NULL);
qb_map_put(map, strdup("testobj.testkey"), strdup("one"));
qb_map_put(map, strdup("testobj.testkey2"), strdup("two"));

iter = qb_map_pref_iter_create(map, "testobj.");
ck_assert(iter != NULL);
res = qb_map_iter_next(iter, (void **)&item);
fprintf(stderr, "%s = %s\n", res, item);
qb_map_iter_free(iter);

item = qb_map_get(map, "testobj.testkey");
ck_assert_str_eq(item, "one");

rc = qb_map_rm(map, "testobj.testkey");
ck_assert(rc == QB_TRUE);

item = qb_map_get(map, "testobj.testkey");
ck_assert(item == NULL);

}
END_TEST


static Suite *
map_suite(void)
{
Expand All @@ -888,6 +922,10 @@ map_suite(void)
tcase_add_test(tc, test_trie_simple);
suite_add_tcase(s, tc);

tc = tcase_create("trie_partial_iterate");
tcase_add_test(tc, test_trie_partial_iterate);
suite_add_tcase(s, tc);

tc = tcase_create("skiplist_remove");
tcase_add_test(tc, test_skiplist_remove);
suite_add_tcase(s, tc);
Expand Down

0 comments on commit 813dfb5

Please sign in to comment.