From f274b5e2002b20b64a84b31791d82e5a195bb771 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 20 Feb 2014 21:27:38 +0100 Subject: [PATCH] data: Correctly format NULL values. --- src/core/data.c | 4 ++++ t/core/data_test.c | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/core/data.c b/src/core/data.c index 500a622..81b511b 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -99,6 +99,10 @@ sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf) sdb_strbuf_append(buf, "%a", datum->data.decimal); break; case SDB_TYPE_STRING: + if (! datum->data.string) { + sdb_strbuf_append(buf, "\"NULL\""); + return 0; + } /* TODO: escape special characters */ sdb_strbuf_append(buf, "\"%s\"", datum->data.string); break; diff --git a/t/core/data_test.c b/t/core/data_test.c index ebff684..a16d7fe 100644 --- a/t/core/data_test.c +++ b/t/core/data_test.c @@ -155,6 +155,17 @@ START_TEST(test_format) "sdb_data_format() used wrong format: %s; expected: %s", string, expected); + datum.data.string = NULL; + sdb_strbuf_clear(buf); + check = sdb_data_format(&datum, buf); + fail_unless(! check, + "sdb_data_format(STRING) = %d; expected: 0", check); + string = sdb_strbuf_string(buf); + expected = "\"NULL\""; + fail_unless(! strcmp(string, expected), + "sdb_data_format() used wrong format: %s; expected: %s", + string, expected); + datum.type = SDB_TYPE_DATETIME; datum.data.datetime = 471147114711471100; sdb_strbuf_clear(buf); @@ -180,6 +191,18 @@ START_TEST(test_format) fail_unless(! strcmp(string, expected), "sdb_data_format() used wrong format: %s; expected: %s", string, expected); + + datum.data.binary.datum = NULL; + datum.data.binary.length = 0; + sdb_strbuf_clear(buf); + check = sdb_data_format(&datum, buf); + fail_unless(! check, + "sdb_data_format(BINARY) = %d; expected: 0", check); + string = sdb_strbuf_string(buf); + expected = "\"\""; + fail_unless(! strcmp(string, expected), + "sdb_data_format() used wrong format: %s; expected: %s", + string, expected); } END_TEST -- 2.30.2