From 0634e5dea88aebd4bd829f36b15d716233a806b9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 21 Oct 2015 16:05:49 +1030 Subject: [PATCH] opt: don't use raw malloc for errors. We should be allocating them with opt's allocator (we use it to free them!). Signed-off-by: Rusty Russell --- ccan/opt/helpers.c | 3 +-- ccan/opt/test/run-set_alloc.c | 9 ++++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ccan/opt/helpers.c b/ccan/opt/helpers.c index f247301c..118e5436 100644 --- a/ccan/opt/helpers.c +++ b/ccan/opt/helpers.c @@ -14,10 +14,9 @@ /* Upper bound to sprintf this simple type? Each 3 bits < 1 digit. */ #define CHAR_SIZE(type) (((sizeof(type)*CHAR_BIT + 2) / 3) + 1) -/* FIXME: asprintf module? */ static char *arg_bad(const char *fmt, const char *arg) { - char *str = malloc(strlen(fmt) + strlen(arg)); + char *str = opt_alloc.alloc(strlen(fmt) + strlen(arg)); sprintf(str, fmt, arg); return str; } diff --git a/ccan/opt/test/run-set_alloc.c b/ccan/opt/test/run-set_alloc.c index b30a77d8..6a10d597 100644 --- a/ccan/opt/test/run-set_alloc.c +++ b/ccan/opt/test/run-set_alloc.c @@ -66,8 +66,9 @@ static void freefn(void *ptr) int main(int argc, char *argv[]) { const char *myname = argv[0]; + unsigned int val; - plan_tests(220); + plan_tests(222); opt_set_alloc(allocfn, reallocfn, freefn); @@ -341,6 +342,12 @@ int main(int argc, char *argv[]) ok1(strcmp(argv[4], "-a") == 0); ok1(!argv[5]); + /* Finally, test the helpers don't use malloc. */ + reset_options(); + opt_register_arg("-a", opt_set_uintval, opt_show_uintval, &val, "a"); + ok1(!parse_args(&argc, &argv, "-a", "notanumber", NULL)); + ok1(strstr(err_output, ": -a: 'notanumber' is not a number")); + /* We should have tested each one at least once! */ ok1(realloc_count); ok1(alloc_count); -- 2.39.2