Skip to content

Commit

Permalink
common: move now-enlarged command_fail_badparam into its own source f…
Browse files Browse the repository at this point in the history
…ile.

It's getting a bit awkward to inline now: it's non-trivial.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jun 24, 2024
1 parent f33c518 commit 9fd29d3
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 102 deletions.
2 changes: 1 addition & 1 deletion common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ COMMON_SRC_NOGEN := \
common/iso4217.c \
common/json_blinded_path.c \
common/json_channel_type.c \
common/json_command.c \
common/json_filter.c \
common/json_param.c \
common/json_parse.c \
Expand Down Expand Up @@ -115,7 +116,6 @@ COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) \
common/gossip_constants.h \
common/hsm_version.h \
common/htlc.h \
common/json_command.h \
common/jsonrpc_errors.h \
common/overflows.h

Expand Down
33 changes: 33 additions & 0 deletions common/json_command.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "config.h"
#include <common/json_command.h>

struct command_result *
command_fail_badparam(struct command *cmd,
const char *paramname,
const char *buffer,
const jsmntok_t *tok,
const char *msg)
{
if (command_dev_apis(cmd)) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"%s: %s: invalid token '%.*s'",
paramname, msg,
json_tok_full_len(tok),
json_tok_full(buffer, tok));
}

/* Someone misconfigured LNBITS with "" around the rune, and so the
* user got a message about a bad rune parameter which *contained the
* rune itself*!. LNBITS should probably swallow any JSONRPC2_* error
* itself, but it is quite possibly not the only case where this case
* where this can happen. So we are a little circumspect in this
* case. */
command_log(cmd, LOG_INFORM,
"Invalid parameter %s (%s): token '%.*s'",
paramname, msg,
json_tok_full_len(tok),
json_tok_full(buffer, tok));
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"%s: %s: invalid token (see logs for details)",
paramname, msg);
}
35 changes: 5 additions & 30 deletions common/json_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,10 @@ struct command_result *command_check_done(struct command *cmd)
WARN_UNUSED_RESULT;

/* Convenient wrapper for "paramname: msg: invalid token '.*%s'" */
static inline struct command_result *
command_fail_badparam(struct command *cmd,
const char *paramname,
const char *buffer,
const jsmntok_t *tok,
const char *msg)
{
if (command_dev_apis(cmd)) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"%s: %s: invalid token '%.*s'",
paramname, msg,
json_tok_full_len(tok),
json_tok_full(buffer, tok));
}

/* Someone misconfigured LNBITS with "" around the rune, and so the
* user got a message about a bad rune parameter which *contained the
* rune itself*!. LNBITS should probably swallow any JSONRPC2_* error
* itself, but it is quite possibly not the only case where this case
* where this can happen. So we are a little circumspect in this
* case. */
command_log(cmd, LOG_INFORM,
"Invalid parameter %s (%s): token '%.*s'",
paramname, msg,
json_tok_full_len(tok),
json_tok_full(buffer, tok));
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"%s: %s: invalid token (see logs for details)",
paramname, msg);
}
struct command_result *command_fail_badparam(struct command *cmd,
const char *paramname,
const char *buffer,
const jsmntok_t *tok,
const char *msg);

#endif /* LIGHTNING_COMMON_JSON_COMMAND_H */
12 changes: 7 additions & 5 deletions common/test/run-json_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_e
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_fail called!\n"); abort(); }
/* Generated stub for command_log */
void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_log called!\n"); abort(); }
/* Generated stub for command_fail_badparam */
struct command_result *command_fail_badparam(struct command *cmd UNNEEDED,
const char *paramname UNNEEDED,
const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char *msg UNNEEDED)
{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); }
/* Generated stub for command_set_usage */
void command_set_usage(struct command *cmd UNNEEDED, const char *usage UNNEEDED)
{ fprintf(stderr, "command_set_usage called!\n"); abort(); }
Expand Down
12 changes: 7 additions & 5 deletions common/test/run-json_remove.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_e
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_fail called!\n"); abort(); }
/* Generated stub for command_log */
void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_log called!\n"); abort(); }
/* Generated stub for command_fail_badparam */
struct command_result *command_fail_badparam(struct command *cmd UNNEEDED,
const char *paramname UNNEEDED,
const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char *msg UNNEEDED)
{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); }
/* Generated stub for command_set_usage */
void command_set_usage(struct command *cmd UNNEEDED, const char *usage UNNEEDED)
{ fprintf(stderr, "command_set_usage called!\n"); abort(); }
Expand Down
20 changes: 7 additions & 13 deletions common/test/run-json_stream-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,16 @@ struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u
/* Generated stub for amount_tx_fee */
struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED)
{ fprintf(stderr, "amount_tx_fee called!\n"); abort(); }
/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_dev_apis called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_fail called!\n"); abort(); }
/* Generated stub for command_fail_badparam */
struct command_result *command_fail_badparam(struct command *cmd UNNEEDED,
const char *paramname UNNEEDED,
const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char *msg UNNEEDED)
{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); }
/* Generated stub for command_filter_ptr */
struct json_filter **command_filter_ptr(struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_filter_ptr called!\n"); abort(); }
/* Generated stub for command_log */
void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_log called!\n"); abort(); }
/* Generated stub for fmt_amount_sat */
char *fmt_amount_sat(const tal_t *ctx UNNEEDED, struct amount_sat sat UNNEEDED)
{ fprintf(stderr, "fmt_amount_sat called!\n"); abort(); }
Expand Down
10 changes: 7 additions & 3 deletions common/test/run-param.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "config.h"
#include "../bech32.c"
#include "../json_command.c"
#include "../json_filter.c"
#include "../json_parse.c"
#include "../json_parse_simple.c"
Expand Down Expand Up @@ -56,14 +57,17 @@ bool command_deprecated_in_ok(struct command *cmd,
return true;
}

/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd)
{
return true;
}

/* AUTOGENERATED MOCKS START */
/* Generated stub for command_check_done */
struct command_result *command_check_done(struct command *cmd)

{ fprintf(stderr, "command_check_done called!\n"); abort(); }
/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_dev_apis called!\n"); abort(); }
/* Generated stub for command_filter_ptr */
struct json_filter **command_filter_ptr(struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_filter_ptr called!\n"); abort(); }
Expand Down
1 change: 1 addition & 0 deletions lightningd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ LIGHTNINGD_COMMON_OBJS := \
common/key_derive.o \
common/keyset.o \
common/json_channel_type.o \
common/json_command.o \
common/json_filter.o \
common/json_param.o \
common/json_parse.o \
Expand Down
15 changes: 7 additions & 8 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,18 @@ bool command_deprecated_out_ok(struct command *cmd UNNEEDED,
const char *depr_start UNNEEDED,
const char *depr_end UNNEEDED)
{ fprintf(stderr, "command_deprecated_out_ok called!\n"); abort(); }
/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_dev_apis called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_fail called!\n"); abort(); }
/* Generated stub for command_fail_badparam */
struct command_result *command_fail_badparam(struct command *cmd UNNEEDED,
const char *paramname UNNEEDED,
const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char *msg UNNEEDED)
{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); }
/* Generated stub for command_failed */
struct command_result *command_failed(struct command *cmd UNNEEDED,
struct json_stream *result)
Expand All @@ -172,11 +176,6 @@ struct command_result *command_failed(struct command *cmd UNNEEDED,
/* Generated stub for command_its_complicated */
struct command_result *command_its_complicated(const char *why UNNEEDED)
{ fprintf(stderr, "command_its_complicated called!\n"); abort(); }
/* Generated stub for command_log */
void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_log called!\n"); abort(); }
/* Generated stub for command_logger */
struct logger *command_logger(struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_logger called!\n"); abort(); }
Expand Down
7 changes: 7 additions & 0 deletions lightningd/test/run-jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
#include <stdio.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for command_fail_badparam */
struct command_result *command_fail_badparam(struct command *cmd UNNEEDED,
const char *paramname UNNEEDED,
const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char *msg UNNEEDED)
{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); }
/* Generated stub for db_begin_transaction_ */
void db_begin_transaction_(struct db *db UNNEEDED, const char *location UNNEEDED)
{ fprintf(stderr, "db_begin_transaction_ called!\n"); abort(); }
Expand Down
20 changes: 7 additions & 13 deletions lightningd/test/run-log-pruning.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
#include <common/setup.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_dev_apis called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_fail called!\n"); abort(); }
/* Generated stub for command_log */
void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_log called!\n"); abort(); }
/* Generated stub for command_fail_badparam */
struct command_result *command_fail_badparam(struct command *cmd UNNEEDED,
const char *paramname UNNEEDED,
const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char *msg UNNEEDED)
{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); }
/* Generated stub for command_param_failed */
struct command_result *command_param_failed(void)

Expand Down
20 changes: 7 additions & 13 deletions lightningd/test/run-log_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ void notify_log(struct lightningd *ld UNNEEDED, const struct log_entry *l UNNEED
{ }

/* AUTOGENERATED MOCKS START */
/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_dev_apis called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_fail called!\n"); abort(); }
/* Generated stub for command_log */
void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_log called!\n"); abort(); }
/* Generated stub for command_fail_badparam */
struct command_result *command_fail_badparam(struct command *cmd UNNEEDED,
const char *paramname UNNEEDED,
const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char *msg UNNEEDED)
{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); }
/* Generated stub for command_param_failed */
struct command_result *command_param_failed(void)

Expand Down
1 change: 1 addition & 0 deletions plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ PLUGIN_COMMON_OBJS := \
common/deprecation.o \
common/features.o \
common/hash_u5.o \
common/json_command.o \
common/json_param.o \
common/json_parse.o \
common/json_parse_simple.o \
Expand Down
7 changes: 7 additions & 0 deletions plugins/bkpr/test/run-bkpr_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
/* Generated stub for account_entry_tag_str */
const char *account_entry_tag_str(enum account_entry_tag tag UNNEEDED)
{ fprintf(stderr, "account_entry_tag_str called!\n"); abort(); }
/* Generated stub for command_fail_badparam */
struct command_result *command_fail_badparam(struct command *cmd UNNEEDED,
const char *paramname UNNEEDED,
const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char *msg UNNEEDED)
{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); }
/* Generated stub for daemon_developer_mode */
bool daemon_developer_mode(char *argv[])
{ fprintf(stderr, "daemon_developer_mode called!\n"); abort(); }
Expand Down
7 changes: 7 additions & 0 deletions plugins/bkpr/test/run-recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
/* Generated stub for account_entry_tag_str */
const char *account_entry_tag_str(enum account_entry_tag tag UNNEEDED)
{ fprintf(stderr, "account_entry_tag_str called!\n"); abort(); }
/* Generated stub for command_fail_badparam */
struct command_result *command_fail_badparam(struct command *cmd UNNEEDED,
const char *paramname UNNEEDED,
const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char *msg UNNEEDED)
{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); }
/* Generated stub for daemon_developer_mode */
bool daemon_developer_mode(char *argv[])
{ fprintf(stderr, "daemon_developer_mode called!\n"); abort(); }
Expand Down
18 changes: 7 additions & 11 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,23 @@ bool command_deprecated_out_ok(struct command *cmd UNNEEDED,
const char *depr_start UNNEEDED,
const char *depr_end UNNEEDED)
{ fprintf(stderr, "command_deprecated_out_ok called!\n"); abort(); }
/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_dev_apis called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_fail called!\n"); abort(); }
/* Generated stub for command_fail_badparam */
struct command_result *command_fail_badparam(struct command *cmd UNNEEDED,
const char *paramname UNNEEDED,
const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char *msg UNNEEDED)
{ fprintf(stderr, "command_fail_badparam called!\n"); abort(); }
/* Generated stub for command_failed */
struct command_result *command_failed(struct command *cmd UNNEEDED,
struct json_stream *result)

{ fprintf(stderr, "command_failed called!\n"); abort(); }
/* Generated stub for command_log */
void command_log(struct command *cmd UNNEEDED, enum log_level level UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_log called!\n"); abort(); }
/* Generated stub for command_param_failed */
struct command_result *command_param_failed(void)

Expand Down Expand Up @@ -599,9 +598,6 @@ bool json_tok_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEE
/* Generated stub for json_tok_full */
const char *json_tok_full(const char *buffer UNNEEDED, const jsmntok_t *t UNNEEDED)
{ fprintf(stderr, "json_tok_full called!\n"); abort(); }
/* Generated stub for json_tok_full_len */
int json_tok_full_len(const jsmntok_t *t UNNEEDED)
{ fprintf(stderr, "json_tok_full_len called!\n"); abort(); }
/* Generated stub for json_tok_streq */
bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED)
{ fprintf(stderr, "json_tok_streq called!\n"); abort(); }
Expand Down

0 comments on commit 9fd29d3

Please sign in to comment.