From: Sebastian Harl Date: Sun, 19 Oct 2014 12:00:41 +0000 (+0200) Subject: data: Let CONCAT return NULL if any of its operands is NULL. X-Git-Tag: sysdb-0.6.0~103 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=32cb89de68b9855c2369edbd389fac7b29678581;p=sysdb.git data: Let CONCAT return NULL if any of its operands is NULL. This mimics behavior in SQL and also allows for missing values to be reported as NULL and propagate that information. --- diff --git a/src/core/data.c b/src/core/data.c index 557dfd6..4840736 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -200,20 +200,21 @@ data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res) else return -1; - if (s1 || s2) { + if (s1 && s2) { new = malloc(len1 + len2 + 1); if (! new) return -1; - } - else - new = NULL; - if (len1) - memcpy(new, s1, len1); - if (len2) - memcpy(new + len1, s2, len2); - if (new) + if (len1) + memcpy(new, s1, len1); + if (len2) + memcpy(new + len1, s2, len2); new[len1 + len2] = '\0'; + } + else { + len1 = len2 = 0; + new = NULL; + } res->type = d1->type; if (res->type == SDB_TYPE_STRING) { diff --git a/t/unit/core/data_test.c b/t/unit/core/data_test.c index a1b4caa..2336c5e 100644 --- a/t/unit/core/data_test.c +++ b/t/unit/core/data_test.c @@ -694,7 +694,7 @@ START_TEST(test_expr_eval) SDB_DATA_INIT, SDB_DATA_INIT, SDB_DATA_INIT, - { SDB_TYPE_STRING, { .string = "" } }, + { SDB_TYPE_STRING, { .string = NULL } }, }, { { SDB_TYPE_STRING, { .string = NULL } }, @@ -714,7 +714,7 @@ START_TEST(test_expr_eval) SDB_DATA_INIT, SDB_DATA_INIT, SDB_DATA_INIT, - { SDB_TYPE_STRING, { .string = "" } }, + { SDB_TYPE_STRING, { .string = NULL } }, }, { { SDB_TYPE_STRING, { .string = "a" } }, @@ -744,7 +744,7 @@ START_TEST(test_expr_eval) SDB_DATA_INIT, SDB_DATA_INIT, SDB_DATA_INIT, - { SDB_TYPE_BINARY, { .binary = { 1, (unsigned char *)"a" } } }, + { SDB_TYPE_BINARY, { .binary = { 0, NULL } } }, }, { { SDB_TYPE_BINARY, { .binary = { 0, NULL } } }, @@ -764,7 +764,7 @@ START_TEST(test_expr_eval) SDB_DATA_INIT, SDB_DATA_INIT, SDB_DATA_INIT, - { SDB_TYPE_BINARY, { .binary = { 1, (unsigned char *)"a" } } }, + { SDB_TYPE_BINARY, { .binary = { 0, NULL } } }, }, { {