]> git.ozlabs.org Git - ccan/commitdiff
opt: Don't segfault if a string's default is NULL
authorDouglas Bagnall <douglas@halo.gen.nz>
Fri, 20 Jun 2014 04:38:33 +0000 (16:38 +1200)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 23 Jun 2014 00:57:23 +0000 (10:27 +0930)
Instead show '(nil)', like other people do. This is distinguishable
from a similar looking string value, because the latter is shown with
double quotes while NULL's nil has no quotes.

Signed-off-by: Douglas Bagnall <douglas@halo.gen.nz>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/opt/helpers.c

index c557f96d8488d01d3e1297ea235f6c2c8b76fb4f..43b86d7ca2ad953665522cf37f449b8046ffd3b7 100644 (file)
@@ -202,14 +202,19 @@ void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b)
 
 void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p)
 {
 
 void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p)
 {
-       size_t len = strlen(*p);
-       buf[0] = '"';
-       if (len > OPT_SHOW_LEN - 2)
-               len = OPT_SHOW_LEN - 2;
-       strncpy(buf+1, *p, len);
-       buf[1+len] = '"';
-       if (len < OPT_SHOW_LEN - 2)
-               buf[2+len] = '\0';
+       if (*p){
+               size_t len = strlen(*p);
+               buf[0] = '"';
+               if (len > OPT_SHOW_LEN - 2)
+                       len = OPT_SHOW_LEN - 2;
+               strncpy(buf+1, *p, len);
+               buf[1+len] = '"';
+               if (len < OPT_SHOW_LEN - 2)
+                       buf[2+len] = '\0';
+       }
+       else {
+               strncpy(buf, "(nil)", OPT_SHOW_LEN);
+       }
 }
 
 /* Show an integer value, various forms. */
 }
 
 /* Show an integer value, various forms. */