From 3f6f9e6fe03697168c18eb361a2b8253aefc58bd Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 2 Jul 2018 10:41:08 +0930 Subject: [PATCH] param: make sure the name is a string literal. We're using a macro anyway, so appending "" make it a compile-time check. Complicates testing a bit, since we actually use generated names there. Signed-off-by: Rusty Russell --- lightningd/params.c | 12 +++++++----- lightningd/params.h | 10 +++++----- lightningd/test/run-params.c | 27 +++++++++------------------ 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/lightningd/params.c b/lightningd/params.c index abf626e45..c999b06d0 100644 --- a/lightningd/params.c +++ b/lightningd/params.c @@ -9,7 +9,7 @@ struct param { const tal_t *ctx; - char *name; + const char *name; bool is_set; param_cb cb; void *arg; @@ -17,7 +17,8 @@ struct param { }; struct param *param_add_(const tal_t *ctx, - char *name, param_cb cb, void *arg, size_t argsize) + const char *name, param_cb cb, void *arg, + size_t argsize) { #if DEVELOPER assert(name); @@ -27,7 +28,7 @@ struct param *param_add_(const tal_t *ctx, struct param *last = tal(tmpctx, struct param); last->ctx = ctx; last->is_set = false; - last->name = tal_strdup(last, name); + last->name = name; last->cb = cb; last->arg = arg; last->argsize = argsize; @@ -37,13 +38,14 @@ struct param *param_add_(const tal_t *ctx, return last; } -struct param *param_opt_add_(const tal_t *ctx, char *name, const jsmntok_t **tok) +struct param *param_opt_add_(const tal_t *ctx, const char *name, + const jsmntok_t **tok) { struct param *last = tal(tmpctx, struct param); assert(ctx); last->ctx = ctx; last->is_set = false; - last->name = tal_strdup(last, name); + last->name = name; last->cb = (param_cb)json_tok_tok; last->arg = tok; last->argsize = sizeof(*tok); diff --git a/lightningd/params.h b/lightningd/params.h index 47e2dc427..b8f26f9d8 100644 --- a/lightningd/params.h +++ b/lightningd/params.h @@ -60,7 +60,7 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg); * Returns an opaque pointer that can be later used in param_is_set(). */ #define param_req(name, cb, arg) \ - param_add_(NULL, name, \ + param_add_(NULL, name"", \ typesafe_cb_preargs(bool, void *, \ (cb), (arg), \ const char *, \ @@ -70,17 +70,17 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg); * Same as above but for optional parameters. */ #define param_opt(ctx, name, cb, arg) \ - param_add_(ctx, name, \ + param_add_(ctx, name"", \ typesafe_cb_preargs(bool, void *, \ (cb), *(arg), \ const char *, \ const jsmntok_t *), \ (arg), sizeof(**arg)) -struct param * param_add_(const tal_t *ctx, char *name, param_cb cb, void *arg, size_t argsize); +struct param * param_add_(const tal_t *ctx, const char *name, param_cb cb, void *arg, size_t argsize); #define param_opt_tok(ctx, name, arg) \ - param_opt_add_(ctx, name, arg) + param_opt_add_(ctx, name"", arg) -struct param *param_opt_add_(const tal_t *ctx, char *name, const jsmntok_t **tok); +struct param *param_opt_add_(const tal_t *ctx, const char *name, const jsmntok_t **tok); #endif /* LIGHTNING_LIGHTNINGD_PARAMS_H */ diff --git a/lightningd/test/run-params.c b/lightningd/test/run-params.c index 939cb708a..ec1dd215e 100644 --- a/lightningd/test/run-params.c +++ b/lightningd/test/run-params.c @@ -318,21 +318,6 @@ static void bad_programmer(void) assert(false); } - /* check for NULL input */ - if (setjmp(jump) == 0) { - param_parse(cmd, j->buffer, j->toks, - param_req(NULL, json_tok_u64, &ival), NULL); - restore_assert(); - assert(false); - } - - if (setjmp(jump) == 0) { - param_parse(cmd, j->buffer, j->toks, - param_req(NULL, json_tok_u64, &ival), NULL); - restore_assert(); - assert(false); - } - if (setjmp(jump) == 0) { param_parse(cmd, j->buffer, j->toks, param_req("u64", NULL, &ival), NULL); @@ -364,12 +349,18 @@ static void add_members(struct param **params, struct json_result *obj, struct json_result *arr, unsigned int *ints) { - char name[256]; for (int i = 0; i < tal_count(ints); ++i) { - sprintf(name, "%d", i); + char *name = tal_fmt(tmpctx, "%i", i); json_add_num(obj, name, i); json_add_num(arr, NULL, i); - params[i] = param_req(name, json_tok_number, &ints[i]); + /* Since name is not a literal, we do this manually. */ + params[i] = param_add_(NULL, name, + typesafe_cb_preargs(bool, void *, + json_tok_number, + &ints[i], + const char *, + const jsmntok_t *), + &ints[i], 0); tal_steal(params, params[i]); } }