Code

data: Fixed copying of NULL data.
authorSebastian Harl <sh@tokkee.org>
Tue, 7 Oct 2014 17:43:24 +0000 (19:43 +0200)
committerSebastian Harl <sh@tokkee.org>
Tue, 7 Oct 2014 17:46:09 +0000 (19:46 +0200)
src/core/data.c
t/unit/core/data_test.c

index 57326fe9c00e8746c3e8e2476f780469813b87e2..9031f03d8a88d4dbd4154c4f55d9400f427c1f30 100644 (file)
@@ -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;
        }
 
index 12944fceb147a13acfd3b7e0f073253a5cf4c89f..88765a47f6176c126eb39858de023f15c926f394 100644 (file)
@@ -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