summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 38228ed)
raw | patch | inline | side by side (parent: 38228ed)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 7 Oct 2014 17:43:24 +0000 (19:43 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Tue, 7 Oct 2014 17:46:09 +0000 (19:46 +0200) |
src/core/data.c | patch | blob | history | |
t/unit/core/data_test.c | patch | blob | history |
diff --git a/src/core/data.c b/src/core/data.c
index 57326fe9c00e8746c3e8e2476f780469813b87e2..9031f03d8a88d4dbd4154c4f55d9400f427c1f30 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
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;
}
index 12944fceb147a13acfd3b7e0f073253a5cf4c89f..88765a47f6176c126eb39858de023f15c926f394 100644 (file)
--- a/t/unit/core/data_test.c
+++ b/t/unit/core/data_test.c
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);
"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