From 9f92af9fab4a57e319e5cd9a6f9ad7eec9852826 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 7 Nov 2024 11:57:28 +1030 Subject: [PATCH] libplugin: use NULL to imply "take prefix from cmd". This avoids jsonrpc_request_start() double-evaluating its cmd arg. Signed-off-by: Rusty Russell --- plugins/commando.c | 7 ++---- plugins/libplugin.c | 39 ++++++++++++++++--------------- plugins/libplugin.h | 17 ++++---------- plugins/test/run-route-calc.c | 3 --- plugins/test/run-route-overlong.c | 3 --- 5 files changed, 27 insertions(+), 42 deletions(-) diff --git a/plugins/commando.c b/plugins/commando.c index f814e1f1e..00d9495d5 100644 --- a/plugins/commando.c +++ b/plugins/commando.c @@ -262,9 +262,7 @@ static struct cond_info *new_cond_info(const tal_t *ctx, cinfo->cmdid_prefix = NULL; incoming->json_id = NULL; } else { - cinfo->cmdid_prefix = tal_fmt(cinfo, "%.*s/", - id->end - id->start, - cinfo->buf + id->start); + cinfo->cmdid_prefix = json_strdup(cinfo, cinfo->buf, id); /* Includes quotes, if any! */ incoming->json_id = tal_strndup(incoming, json_tok_full(cinfo->buf, id), @@ -728,8 +726,7 @@ static struct command_result *forward_command(struct command *cmd, /* params could be an array, so use low-level helper */ struct out_req *req; - req = jsonrpc_request_whole_object_start(cmd, method, - json_id_prefix(tmpctx, cmd), + req = jsonrpc_request_whole_object_start(cmd, method, NULL, forward_reply, NULL); json_add_tok(req->js, "params", params, buffer); return send_outreq(req); diff --git a/plugins/libplugin.c b/plugins/libplugin.c index 6c7d5891d..a912082b7 100644 --- a/plugins/libplugin.c +++ b/plugins/libplugin.c @@ -273,25 +273,26 @@ static void disable_request_cb(struct command *cmd, struct out_req *out) out->cmd = NULL; } -const char *json_id_prefix(const tal_t *ctx, const struct command *cmd) +/* Prefix is usually a cmd->id */ +static const char *json_id(const tal_t *ctx, struct plugin *plugin, + const char *method, const char *prefix) { - /* Strip quotes! */ - if (strstarts(cmd->id, "\"")) { - assert(strlen(cmd->id) >= 2); - assert(strends(cmd->id, "\"")); - return tal_fmt(ctx, "%.*s/", - (int)strlen(cmd->id) - 2, cmd->id + 1); - } - return tal_fmt(ctx, "%s/", cmd->id); -} + const char *rawid; + int rawidlen; -static const char *append_json_id(const tal_t *ctx, - struct plugin *plugin, - const char *method, - const char *prefix) -{ - return tal_fmt(ctx, "\"%s%s:%s#%"PRIu64"\"", - prefix, plugin->id, method, plugin->next_outreq_id++); + /* Strip quotes! */ + if (strstarts(prefix, "\"")) { + assert(strlen(prefix) >= 2); + assert(strends(prefix, "\"")); + rawid = prefix + 1; + rawidlen = strlen(prefix) - 2; + } else { + rawid = prefix; + rawidlen = strlen(prefix); + } + + return tal_fmt(ctx, "\"%.*s/%s:%s#%"PRIu64"\"", + rawidlen, rawid, plugin->id, method, plugin->next_outreq_id++); } static void destroy_out_req(struct out_req *out_req, struct plugin *plugin) @@ -320,7 +321,7 @@ jsonrpc_request_start_(struct command *cmd, assert(cmd); out = tal(cmd, struct out_req); - out->id = append_json_id(out, cmd->plugin, method, id_prefix); + out->id = json_id(out, cmd->plugin, method, id_prefix ? id_prefix : cmd->id); out->cmd = cmd; out->cb = cb; out->errcb = errcb; @@ -704,7 +705,7 @@ static const jsmntok_t *sync_req(const tal_t *ctx, const jsmntok_t *contents; int reqlen; struct json_out *jout = json_out_new(tmpctx); - const char *id = append_json_id(tmpctx, plugin, method, "init/"); + const char *id = json_id(tmpctx, plugin, "init/", method); json_out_start(jout, NULL, '{'); json_out_addstr(jout, "jsonrpc", "2.0"); diff --git a/plugins/libplugin.h b/plugins/libplugin.h index af9728bd9..7d13ae226 100644 --- a/plugins/libplugin.h +++ b/plugins/libplugin.h @@ -123,13 +123,10 @@ struct out_req *jsonrpc_request_start_(struct command *cmd, const jsmntok_t *result, void *arg), void *arg) - NON_NULL_ARGS(1, 2, 3, 5); + NON_NULL_ARGS(1, 2, 5); -/* This variant has callbacks received whole obj, not "result" or - * "error" members. */ -#define jsonrpc_request_start(cmd, method, cb, errcb, arg) \ - jsonrpc_request_start_((cmd), (method), \ - json_id_prefix(tmpctx, (cmd)), NULL, \ +#define jsonrpc_request_start(cmd, method, cb, errcb, arg) \ + jsonrpc_request_start_((cmd), (method), NULL, NULL, \ typesafe_cb_preargs(struct command_result *, void *, \ (cb), (arg), \ struct command *command, \ @@ -143,8 +140,7 @@ struct out_req *jsonrpc_request_start_(struct command *cmd, (arg)) #define jsonrpc_request_with_filter_start(cmd, method, filter, cb, errcb, arg) \ - jsonrpc_request_start_((cmd), (method), \ - json_id_prefix(tmpctx, (cmd)), (filter), \ + jsonrpc_request_start_((cmd), (method), NULL, (filter), \ typesafe_cb_preargs(struct command_result *, void *, \ (cb), (arg), \ struct command *command, \ @@ -160,7 +156,7 @@ struct out_req *jsonrpc_request_start_(struct command *cmd, /* This variant has callbacks received whole obj, not "result" or * "error" members. It also doesn't start params{}. */ #define jsonrpc_request_whole_object_start(cmd, method, id_prefix, cb, arg) \ - jsonrpc_request_start_((cmd), (method), (id_prefix), NULL, \ + jsonrpc_request_start_((cmd), (method), (id_prefix), NULL, \ typesafe_cb_preargs(struct command_result *, void *, \ (cb), (arg), \ struct command *command, \ @@ -641,9 +637,6 @@ struct createonion_response *json_to_createonion_response(const tal_t *ctx, struct route_hop *json_to_route(const tal_t *ctx, const char *buffer, const jsmntok_t *toks); -/* Create a prefix (ending in /) for this cmd_id, if any. */ -const char *json_id_prefix(const tal_t *ctx, const struct command *cmd); - void plugin_set_memleak_handler(struct plugin *plugin, void (*mark_mem)(struct plugin *plugin, struct htable *memtable)); diff --git a/plugins/test/run-route-calc.c b/plugins/test/run-route-calc.c index ee5859e2d..f6a68ff64 100644 --- a/plugins/test/run-route-calc.c +++ b/plugins/test/run-route-calc.c @@ -161,9 +161,6 @@ void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNN const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED, const char *label UNNEEDED) { fprintf(stderr, "json_get_member called!\n"); abort(); } -/* Generated stub for json_id_prefix */ -const char *json_id_prefix(const tal_t *ctx UNNEEDED, const struct command *cmd UNNEEDED) -{ fprintf(stderr, "json_id_prefix called!\n"); abort(); } /* Generated stub for json_next */ const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED) { fprintf(stderr, "json_next called!\n"); abort(); } diff --git a/plugins/test/run-route-overlong.c b/plugins/test/run-route-overlong.c index fc5a40c5b..3a7e6888e 100644 --- a/plugins/test/run-route-overlong.c +++ b/plugins/test/run-route-overlong.c @@ -158,9 +158,6 @@ void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNN const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED, const char *label UNNEEDED) { fprintf(stderr, "json_get_member called!\n"); abort(); } -/* Generated stub for json_id_prefix */ -const char *json_id_prefix(const tal_t *ctx UNNEEDED, const struct command *cmd UNNEEDED) -{ fprintf(stderr, "json_id_prefix called!\n"); abort(); } /* Generated stub for json_next */ const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED) { fprintf(stderr, "json_next called!\n"); abort(); }