]> git.ozlabs.org Git - ccan/commitdiff
bytestring: Allow bytestring_from_string to be called on NULL
authorDavid Gibson <david@gibson.dropbear.id.au>
Mon, 8 Oct 2012 08:05:48 +0000 (19:05 +1100)
committerDavid Gibson <david@gibson.dropbear.id.au>
Mon, 8 Oct 2012 10:44:00 +0000 (21:44 +1100)
Currently, calling bytestring_from_string(NULL) will result in a SEGV
within strlen().  This patch makes this construct safe, returning
bytestring_NULL, which seems a less surprising result.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/bytestring/bytestring.h
ccan/bytestring/test/run.c

index ee4087d44cb4c9dc864e632973e917cafb768ea9..63472fe24d6cdfd6e84802f6103b2658a610b098 100644 (file)
@@ -62,6 +62,8 @@ static inline struct bytestring bytestring(const char *p, size_t l)
  */
 static inline struct bytestring bytestring_from_string(const char *s)
 {
  */
 static inline struct bytestring bytestring_from_string(const char *s)
 {
+       if (!s)
+               return bytestring_NULL;
        return bytestring(s, strlen(s));
 }
 
        return bytestring(s, strlen(s));
 }
 
index 49c16a6a80e37398525262415351f53e8d638aa5..9423b9a0bd72b094a570f34751de660027fda488 100644 (file)
@@ -9,10 +9,10 @@ const char *str2 = TEST_STRING;
 
 int main(void)
 {
 
 int main(void)
 {
-       struct bytestring bs, bs1, bs2, bs3, bs4;
+       struct bytestring bs, bs1, bs2, bs3, bs4, bs5;
 
        /* This is how many tests you plan to run */
 
        /* This is how many tests you plan to run */
-       plan_tests(6);
+       plan_tests(9);
 
        bs = bytestring(str1, sizeof(str1) - 1);
        ok1(bs.ptr == str1);
 
        bs = bytestring(str1, sizeof(str1) - 1);
        ok1(bs.ptr == str1);
@@ -30,6 +30,11 @@ int main(void)
        bs4 = bytestring_from_string(TEST_STRING_2);
        ok1(bs4.len == 3);
 
        bs4 = bytestring_from_string(TEST_STRING_2);
        ok1(bs4.len == 3);
 
+       bs5 = bytestring_from_string(NULL);
+       ok1(bs5.len == 0);
+       ok1(bs5.ptr == NULL);
+       ok1(bytestring_eq(bs5, bytestring_NULL));
+
        /* This exits depending on whether all tests passed */
        return exit_status();
 }
        /* This exits depending on whether all tests passed */
        return exit_status();
 }