From 5317c029d56706658138ef3ad0a3e51040386601 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 13 Feb 2016 21:38:42 +0100 Subject: [PATCH] fix key upgrade. should have had a test to begin with. github issue #481 --- src/attributes/key.c | 2 ++ src/attributes/key.test.c | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/attributes/key.c b/src/attributes/key.c index ed6386d5f..4f51f81d5 100644 --- a/src/attributes/key.c +++ b/src/attributes/key.c @@ -77,6 +77,7 @@ void a_upgradekeys(attrib **alist, attrib *abegin) { keys = realloc(keys, sizeof(int) * (n + i + 1)); memcpy(keys + n + 1, val, sizeof(int)*i); n += i; + i = 0; } } if (i > 0) { @@ -84,6 +85,7 @@ void a_upgradekeys(attrib **alist, attrib *abegin) { memcpy(keys + n + 1, val, sizeof(int)*i); } keys[0] = n + i; + a->data.v = keys; } attrib_type at_key = { diff --git a/src/attributes/key.test.c b/src/attributes/key.test.c index e15c50c3b..72b72a3b9 100644 --- a/src/attributes/key.test.c +++ b/src/attributes/key.test.c @@ -19,9 +19,32 @@ static void test_get_set_keys(CuTest *tc) { a_removeall(&a, NULL); } +static attrib *key_set_orig(attrib **alist, int key) { + attrib * a = a_add(alist, a_new(&at_key)); + a->data.i = key; + return a; +} + +static void test_upgrade(CuTest *tc) { + attrib *alist = 0; + key_set_orig(&alist, 40); + key_set_orig(&alist, 41); + key_set_orig(&alist, 42); + key_set_orig(&alist, 43); + key_set_orig(&alist, 44); + CuAssertPtrNotNull(tc, alist->type->upgrade); + alist->type->upgrade(&alist, alist); + CuAssertTrue(tc, key_get(alist, 40)); + CuAssertTrue(tc, key_get(alist, 41)); + CuAssertTrue(tc, key_get(alist, 42)); + CuAssertTrue(tc, key_get(alist, 43)); + CuAssertTrue(tc, key_get(alist, 44)); +} + CuSuite *get_key_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_get_set_keys); + SUITE_ADD_TEST(suite, test_upgrade); return suite; }