Skip to content

Commit

Permalink
cli: don't crash when there's no argument
Browse files Browse the repository at this point in the history
This should provide the default help message and exit, but was
resulting in a segmentation fault from freeing pointers passed to
the default config.

Changelog-Fixed: lightning-cli properly returns help without argument
  • Loading branch information
endothermicdev authored and rustyrussell committed Aug 16, 2023
1 parent 5531c9d commit bd4a001
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
5 changes: 3 additions & 2 deletions cli/lightning-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ int main(int argc, char *argv[])
jsmntok_t *toks;
const jsmntok_t *result, *error, *id;
const tal_t *ctx = tal(NULL, char);
char *net_dir, *rpc_filename;
char *config_filename, *base_dir, *net_dir, *rpc_filename;
jsmn_parser parser;
int parserr;
enum format format = DEFAULT_FORMAT;
Expand All @@ -668,7 +668,8 @@ int main(int argc, char *argv[])
setup_option_allocators();

opt_exitcode = ERROR_USAGE;
minimal_config_opts(ctx, argc, argv, &net_dir, &rpc_filename);
minimal_config_opts(ctx, argc, argv, &config_filename, &base_dir,
&net_dir, &rpc_filename);

opt_register_noarg("--help|-h", opt_usage_and_exit,
"<command> [<params>...]", "Show this message. Use the command help (without hyphens -- \"lightning-cli help\") to get a list of all RPC commands");
Expand Down
10 changes: 6 additions & 4 deletions common/configdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,18 @@ static struct configvar **gather_cmdline_args(const tal_t *ctx,

void minimal_config_opts(const tal_t *ctx,
int argc, char *argv[],
char **config_filename,
char **basedir,
char **config_netdir,
char **rpc_filename)
{
char *unused_filename, *unused_basedir;

initial_config_opts(tmpctx, &argc, argv, false,
&unused_filename,
&unused_basedir,
config_filename,
basedir,
config_netdir,
rpc_filename);
tal_steal(ctx, *config_filename);
tal_steal(ctx, *basedir);
tal_steal(ctx, *config_netdir);
tal_steal(ctx, *rpc_filename);
}
Expand Down
2 changes: 2 additions & 0 deletions common/configdir.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ void setup_option_allocators(void);
/* Minimal config parsing for tools: use opt_early_parse/opt_parse after */
void minimal_config_opts(const tal_t *ctx,
int argc, char *argv[],
char **config_filename,
char **basedir,
char **config_netdir,
char **rpc_filename);

Expand Down
4 changes: 3 additions & 1 deletion devtools/checkchannels.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ static void copy_column(void *dst, size_t size,

int main(int argc, char *argv[])
{
char *config_filename, *base_dir;
char *net_dir, *rpc_filename, *hsmfile, *dbfile;
sqlite3 *sql;
sqlite3_stmt *stmt;
Expand All @@ -124,7 +125,8 @@ int main(int argc, char *argv[])

setup_option_allocators();

minimal_config_opts(top_ctx, argc, argv, &net_dir, &rpc_filename);
minimal_config_opts(top_ctx, argc, argv, &config_filename, &base_dir,
&net_dir, &rpc_filename);

opt_register_noarg("-v|--verbose", opt_set_bool, &verbose,
"Print everything");
Expand Down

0 comments on commit bd4a001

Please sign in to comment.