mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
jsonrpc: Use tal_arr_remove instead of leaving NULL in the commands
Suggested-by: Rusty Russell <@rustyrussell> Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
committed by
Rusty Russell
parent
230730eca4
commit
b23a33ec7a
@@ -347,11 +347,8 @@ static const struct json_command *find_cmd(const struct jsonrpc *rpc,
|
|||||||
{
|
{
|
||||||
struct json_command **commands = rpc->commands;
|
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++)
|
for (size_t i = 0; i < tal_count(commands); i++)
|
||||||
if (commands[i] && commands[i]->name &&
|
if (json_tok_streq(buffer, tok, commands[i]->name))
|
||||||
json_tok_streq(buffer, tok, commands[i]->name))
|
|
||||||
return commands[i];
|
return commands[i];
|
||||||
return NULL;
|
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 */
|
/* Check that we don't clobber a method */
|
||||||
for (size_t i = 0; i < count; i++)
|
for (size_t i = 0; i < count; i++)
|
||||||
if (rpc->commands[i] != NULL &&
|
if (streq(rpc->commands[i]->name, command->name))
|
||||||
streq(rpc->commands[i]->name, command->name))
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
*tal_arr_expand(&rpc->commands) = command;
|
*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)
|
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; i<tal_count(rpc->commands); i++) {
|
for (size_t i=0; i<tal_count(rpc->commands); i++) {
|
||||||
struct json_command *cmd = rpc->commands[i];
|
struct json_command *cmd = rpc->commands[i];
|
||||||
if (cmd && streq(cmd->name, method)) {
|
if (streq(cmd->name, method)) {
|
||||||
rpc->commands[i] = tal_free(cmd);
|
tal_arr_remove(&rpc->commands, i);
|
||||||
|
tal_free(cmd);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
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 */
|
/* Remove js and shift all remainig over */
|
||||||
tal_free(plugin->js_arr[0]);
|
tal_arr_remove(&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);
|
|
||||||
|
|
||||||
return plugin_write_json(conn, plugin);
|
return plugin_write_json(conn, plugin);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user