summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Bagnall <douglas@halo.gen.nz>2014-06-20 16:38:33 +1200
committerRusty Russell <rusty@rustcorp.com.au>2014-06-23 10:27:23 +0930
commit7220240c0e2fa8c9610736565e73d3c74a73466c (patch)
treef24d8bfc4a30dec5a0669800285d9d8a36371a06
parent63b0055673d2c55ff90f8b7bd8a7c5fe30f83269 (diff)
opt: Don't segfault if a string's default is NULL
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>
-rw-r--r--ccan/opt/helpers.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/ccan/opt/helpers.c b/ccan/opt/helpers.c
index c557f96d..43b86d7c 100644
--- a/ccan/opt/helpers.c
+++ b/ccan/opt/helpers.c
@@ -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)
{
- 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. */