Code

data: Format NULL values as "<NULL>".
authorSebastian Harl <sh@tokkee.org>
Sun, 5 Oct 2014 18:23:26 +0000 (20:23 +0200)
committerSebastian Harl <sh@tokkee.org>
Sun, 5 Oct 2014 18:23:26 +0000 (20:23 +0200)
src/core/data.c
t/unit/core/data_test.c

index 1188b4030ba4215304c4f7a47ae25e68a5feb49f..a57c8c6affc739a2e95bad3c0bd9dc08442c5088 100644 (file)
@@ -372,13 +372,15 @@ sdb_data_strlen(const sdb_data_t *datum)
                        return 42;
                case SDB_TYPE_STRING:
                        if (! datum->data.string)
-                               return 6; /* "NULL" */
+                               return 8; /* "<NULL>" */
                        /* in the worst case, each character needs to be escaped */
                        return 2 * strlen(datum->data.string) + 2;
                case SDB_TYPE_DATETIME:
                        /* "YYYY-MM-DD HH:MM:SS +zzzz" */
                        return 27;
                case SDB_TYPE_BINARY:
+                       if (! datum->data.binary.datum)
+                               return 8; /* "<NULL>" */
                        /* "\xNN" */
                        return 4 * datum->data.binary.length + 2;
        }
@@ -406,7 +408,7 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
                        break;
                case SDB_TYPE_STRING:
                        if (! datum->data.string)
-                               data = "NULL";
+                               data = "<NULL>";
                        else {
                                pos = 0;
                                for (i = 0; i < strlen(datum->data.string); ++i) {
@@ -448,8 +450,12 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
                                tmp[pos] = hex[byte & 0xf];
                                ++pos;
                        }
-                       tmp[pos] = '\0';
-                       data = tmp;
+                       if (datum->data.binary.datum) {
+                               tmp[pos] = '\0';
+                               data = tmp;
+                       }
+                       else
+                               data = "<NULL>";
                        break;
        }
 
index 606eae53b1cf69415eb2076a8c868be0ae890f40..6f9376ff48fdd5ad4a22b247bca20c6b5683dc41 100644 (file)
@@ -537,7 +537,7 @@ START_TEST(test_format)
                },
                {
                        { SDB_TYPE_STRING, { .string = NULL } },
-                       "\"NULL\"",
+                       "\"<NULL>\"",
                },
                {
                        { SDB_TYPE_STRING, { .string = "this is a test" } },
@@ -553,7 +553,7 @@ START_TEST(test_format)
                },
                {
                        { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
-                       "\"\"",
+                       "\"<NULL>\"",
                },
                {
                        {