From: Sebastian Harl Date: Tue, 7 Oct 2014 17:43:24 +0000 (+0200) Subject: data: Fixed copying of NULL data. X-Git-Tag: sysdb-0.6.0~136 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=99e629dac9253862563ad58a05c85334cf5e37f5;p=sysdb.git data: Fixed copying of NULL data. --- diff --git a/src/core/data.c b/src/core/data.c index 57326fe..9031f03 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -241,16 +241,20 @@ sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src) tmp = *src; switch (src->type) { case SDB_TYPE_STRING: - tmp.data.string = strdup(src->data.string); - if (! tmp.data.string) - return -1; + if (src->data.string) { + tmp.data.string = strdup(src->data.string); + if (! tmp.data.string) + return -1; + } break; case SDB_TYPE_BINARY: - tmp.data.binary.datum = malloc(src->data.binary.length); - if (! tmp.data.binary.datum) - return -1; - memcpy(tmp.data.binary.datum, src->data.binary.datum, - src->data.binary.length); + if (src->data.binary.datum) { + tmp.data.binary.datum = malloc(src->data.binary.length); + if (! tmp.data.binary.datum) + return -1; + memcpy(tmp.data.binary.datum, src->data.binary.datum, + src->data.binary.length); + } break; } diff --git a/t/unit/core/data_test.c b/t/unit/core/data_test.c index 12944fc..88765a4 100644 --- a/t/unit/core/data_test.c +++ b/t/unit/core/data_test.c @@ -73,6 +73,22 @@ START_TEST(test_data) fail_unless(d1.data.string == NULL, "sdb_data_free_datum() didn't free string data"); + d1.type = 0; + d2.type = SDB_TYPE_STRING; + d2.data.string = NULL; + check = sdb_data_copy(&d1, &d2); + fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check); + fail_unless(d1.type == d2.type, + "sdb_data_copy() didn't copy type; got: %i; expected: %i", + d1.type, d2.type); + fail_unless(d1.data.string == d2.data.string, + "sdb_data_copy() didn't copy string data: got: %s; expected: %s", + d1.data.string, d2.data.string); + + sdb_data_free_datum(&d1); + fail_unless(d1.data.string == NULL, + "sdb_data_free_datum() didn't free string data"); + d2.type = SDB_TYPE_DATETIME; d2.data.datetime = 4711; check = sdb_data_copy(&d1, &d2); @@ -105,6 +121,28 @@ START_TEST(test_data) "sdb_data_free_datum() didn't reset binary datum length"); fail_unless(d1.data.binary.datum == NULL, "sdb_data_free_datum() didn't free binary datum"); + + d1.type = 0; + d2.type = SDB_TYPE_BINARY; + d2.data.binary.datum = NULL; + d2.data.binary.length = 0; + check = sdb_data_copy(&d1, &d2); + fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check); + fail_unless(d1.type == d2.type, + "sdb_data_copy() didn't copy type; got: %i; expected: %i", + d1.type, d2.type); + fail_unless(d1.data.binary.length == d2.data.binary.length, + "sdb_data_copy() didn't copy length; got: %d; expected: 5d", + d1.data.binary.length, d2.data.binary.length); + fail_unless(d1.data.binary.datum == d2.data.binary.datum, + "sdb_data_copy() didn't copy binary data: got: %s; expected: %s", + d1.data.string, d2.data.string); + + sdb_data_free_datum(&d1); + fail_unless(d1.data.binary.length == 0, + "sdb_data_free_datum() didn't reset binary datum length"); + fail_unless(d1.data.binary.datum == NULL, + "sdb_data_free_datum() didn't free binary datum"); } END_TEST