From b23a33ec7a147ef5a40d94843a0457df78cb986d Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 3 Dec 2018 15:14:43 +0100 Subject: [PATCH] jsonrpc: Use tal_arr_remove instead of leaving NULL in the commands Suggested-by: Rusty Russell <@rustyrussell> Signed-off-by: Christian Decker --- lightningd/jsonrpc.c | 16 ++++++---------- lightningd/plugin.c | 6 ++---- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 17b88550e..f29aeec62 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -347,11 +347,8 @@ static const struct json_command *find_cmd(const struct jsonrpc *rpc, { struct json_command **commands = rpc->commands; - /* commands[i] can be NULL if the plugin that registered it - * was killed, commands[i]->name can be NULL in test code. */ for (size_t i = 0; i < tal_count(commands); i++) - if (commands[i] && commands[i]->name && - json_tok_streq(buffer, tok, commands[i]->name)) + if (json_tok_streq(buffer, tok, commands[i]->name)) return commands[i]; return NULL; } @@ -731,8 +728,7 @@ bool jsonrpc_command_add(struct jsonrpc *rpc, struct json_command *command) /* Check that we don't clobber a method */ for (size_t i = 0; i < count; i++) - if (rpc->commands[i] != NULL && - streq(rpc->commands[i]->name, command->name)) + if (streq(rpc->commands[i]->name, command->name)) return false; *tal_arr_expand(&rpc->commands) = command; @@ -741,12 +737,12 @@ bool jsonrpc_command_add(struct jsonrpc *rpc, struct json_command *command) void jsonrpc_command_remove(struct jsonrpc *rpc, const char *method) { - // FIXME: Currently leaves NULL entries in the table, if we - // restart plugins we should shift them out. for (size_t i=0; icommands); i++) { struct json_command *cmd = rpc->commands[i]; - if (cmd && streq(cmd->name, method)) { - rpc->commands[i] = tal_free(cmd); + if (streq(cmd->name, method)) { + tal_arr_remove(&rpc->commands, i); + tal_free(cmd); + break; } } } diff --git a/lightningd/plugin.c b/lightningd/plugin.c index 33dab208b..d0ceb1cc4 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -360,11 +360,9 @@ static struct io_plan *plugin_write_json(struct io_conn *conn, static struct io_plan *plugin_stream_complete(struct io_conn *conn, struct json_stream *js, struct plugin *plugin) { - size_t pending = tal_count(plugin->js_arr); + assert(tal_count(plugin->js_arr) > 0); /* Remove js and shift all remainig over */ - tal_free(plugin->js_arr[0]); - memmove(plugin->js_arr, plugin->js_arr + 1, (pending - 1) * sizeof(plugin->js_arr[0])); - tal_resize(&plugin->js_arr, pending-1); + tal_arr_remove(&plugin->js_arr, 0); return plugin_write_json(conn, plugin); }