Browse Source

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 <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
parent
commit
3f6f9e6fe0
  1. 12
      lightningd/params.c
  2. 10
      lightningd/params.h
  3. 27
      lightningd/test/run-params.c

12
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);

10
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 */

27
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]);
}
}

Loading…
Cancel
Save