From 8ebc95b7b023c384e2854dcedd59ce00e7c173db Mon Sep 17 00:00:00 2001 From: Mark Beckwith Date: Mon, 13 Aug 2018 15:31:40 -0500 Subject: [PATCH] param: upgraded json_tok_bool Signed-off-by: Mark Beckwith --- common/json.c | 17 ----------------- common/json.h | 3 --- lightningd/json.c | 23 +++++++++++++++++++++++ lightningd/json.h | 5 +++++ lightningd/param.c | 1 - lightningd/peer_control.c | 19 ++++++------------- lightningd/peer_htlcs.c | 6 +++--- wallet/test/run-wallet.c | 4 +++- 8 files changed, 40 insertions(+), 38 deletions(-) diff --git a/common/json.c b/common/json.c index 1383b877d..71dc9b382 100644 --- a/common/json.c +++ b/common/json.c @@ -141,23 +141,6 @@ bool json_tok_is_null(const char *buffer, const jsmntok_t *tok) return buffer[tok->start] == 'n'; } -bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b) -{ - if (tok->type != JSMN_PRIMITIVE) - return false; - if (tok->end - tok->start == strlen("true") - && memcmp(buffer + tok->start, "true", strlen("true")) == 0) { - *b = true; - return true; - } - if (tok->end - tok->start == strlen("false") - && memcmp(buffer + tok->start, "false", strlen("false")) == 0) { - *b = false; - return true; - } - return false; -} - bool json_tok_sha256(const char *buffer, const jsmntok_t * tok, struct sha256 *hash) { diff --git a/common/json.h b/common/json.h index fc175dd5d..6a6b75abd 100644 --- a/common/json.h +++ b/common/json.h @@ -42,9 +42,6 @@ bool json_tok_bitcoin_amount(const char *buffer, const jsmntok_t *tok, /* Extract double in range [0.0, 100.0] */ bool json_tok_percent(const char *buffer, const jsmntok_t *tok, double *num); -/* Extract boolean this (must be a true or false) */ -bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b); - /* Extract sha256 hash */ bool json_tok_sha256(const char *buffer, const jsmntok_t * tok, struct sha256 *hash); diff --git a/lightningd/json.c b/lightningd/json.c index 2488c4a95..1d399da03 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -91,6 +91,29 @@ void json_add_txid(struct json_result *result, const char *fieldname, json_add_string(result, fieldname, hex); } +bool json_tok_bool(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + bool **b) +{ + *b = tal(cmd, bool); + if (tok->type == JSMN_PRIMITIVE) { + if (tok->end - tok->start == strlen("true") + && !memcmp(buffer + tok->start, "true", strlen("true"))) { + **b = true; + return true; + } + if (tok->end - tok->start == strlen("false") + && !memcmp(buffer + tok->start, "false", strlen("false"))) { + **b = false; + return true; + } + } + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be 'true' or 'false', not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; +} + bool json_tok_double(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, double **num) diff --git a/lightningd/json.h b/lightningd/json.h index fc4acee44..f0d526e85 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -41,6 +41,11 @@ void json_add_pubkey(struct json_result *response, void json_add_txid(struct json_result *result, const char *fieldname, const struct bitcoin_txid *txid); +/* Extract boolean this (must be a true or false) */ +bool json_tok_bool(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + bool **b); + /* Extract double from this (must be a number literal) */ bool json_tok_double(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, diff --git a/lightningd/param.c b/lightningd/param.c index 4e9c12f37..376c6d7e2 100644 --- a/lightningd/param.c +++ b/lightningd/param.c @@ -50,7 +50,6 @@ struct fail_format { }; static struct fail_format fail_formats[] = { - {json_tok_bool, "'%s' should be 'true' or 'false', not '%.*s'"}, {json_tok_percent, "'%s' should be a double in range [0.0, 100.0], not '%.*s'"}, {json_tok_newaddr, "'%s' should be 'bech32' or 'p2sh-segwit', not '%.*s'"}, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 3dda6d258..ec7bcae01 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -833,11 +833,11 @@ static void json_close(struct command *cmd, struct peer *peer; struct channel *channel; unsigned int *timeout; - bool force; + bool *force; if (!param(cmd, buffer, params, p_req_tal("id", json_tok_tok, &idtok), - p_opt_def("force", json_tok_bool, &force, false), + p_opt_def_tal("force", json_tok_bool, &force, false), p_opt_def_tal("timeout", json_tok_number, &timeout, 30), NULL)) return; @@ -891,7 +891,7 @@ static void json_close(struct command *cmd, } /* Register this command for later handling. */ - register_close_command(cmd->ld, cmd, channel, *timeout, force); + register_close_command(cmd->ld, cmd, channel, *timeout, *force); /* Wait until close drops down to chain. */ command_still_pending(cmd); @@ -1169,22 +1169,15 @@ static void json_dev_forget_channel(struct command *cmd, const char *buffer, struct dev_forget_channel_cmd *forget = tal(cmd, struct dev_forget_channel_cmd); forget->cmd = cmd; - /* If &forget->force is used directly in p_opt_def() below then - * gcc 7.3.0 fails with: - * 'operation on ‘forget->force’ may be undefined [-Werror=sequence-point]' - * - * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86584 - * - * Hence this indirection. - */ - bool *force = &forget->force; + bool *force; if (!param(cmd, buffer, params, p_req("id", json_tok_pubkey, &peerid), p_opt("short_channel_id", json_tok_short_channel_id, &scid), - p_opt_def("force", json_tok_bool, force, false), + p_opt_def_tal("force", json_tok_bool, &force, false), NULL)) return; + forget->force = *force; peer = peer_by_id(cmd->ld, &peerid); if (!peer) { command_fail(cmd, LIGHTNINGD, diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 5187138c2..52635efe4 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1695,11 +1695,11 @@ static void json_dev_ignore_htlcs(struct command *cmd, const char *buffer, { struct pubkey peerid; struct peer *peer; - bool ignore; + bool *ignore; if (!param(cmd, buffer, params, p_req("id", json_tok_pubkey, &peerid), - p_req("ignore", json_tok_bool, &ignore), + p_req_tal("ignore", json_tok_bool, &ignore), NULL)) return; @@ -1709,7 +1709,7 @@ static void json_dev_ignore_htlcs(struct command *cmd, const char *buffer, "Could not find channel with that peer"); return; } - peer->ignore_htlcs = ignore; + peer->ignore_htlcs = *ignore; command_success(cmd, null_response(cmd)); } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index c04f91c91..70dcfacc9 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -218,7 +218,9 @@ void json_object_end(struct json_result *ptr UNNEEDED) void json_object_start(struct json_result *ptr UNNEEDED, const char *fieldname UNNEEDED) { fprintf(stderr, "json_object_start called!\n"); abort(); } /* Generated stub for json_tok_bool */ -bool json_tok_bool(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, bool *b UNNEEDED) +bool json_tok_bool(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + bool **b UNNEEDED) { fprintf(stderr, "json_tok_bool called!\n"); abort(); } /* Generated stub for json_tok_channel_id */ bool json_tok_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,