Skip to content

Commit 0b4a1ba

Browse files
committed
libplugin: use NULL to imply "take prefix from cmd".
This avoids jsonrpc_request_start() double-evaluating its cmd arg. Signed-off-by: Rusty Russell <[email protected]>
1 parent 218aa6c commit 0b4a1ba

File tree

5 files changed

+24
-37
lines changed

5 files changed

+24
-37
lines changed

plugins/commando.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,7 @@ static struct command_result *forward_command(struct command *cmd,
728728
/* params could be an array, so use low-level helper */
729729
struct out_req *req;
730730

731-
req = jsonrpc_request_whole_object_start(cmd, method,
732-
json_id_prefix(tmpctx, cmd),
731+
req = jsonrpc_request_whole_object_start(cmd, method, NULL,
733732
forward_reply, NULL);
734733
json_add_tok(req->js, "params", params, buffer);
735734
return send_outreq(req);

plugins/libplugin.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -273,25 +273,26 @@ static void disable_request_cb(struct command *cmd, struct out_req *out)
273273
out->cmd = NULL;
274274
}
275275

276-
const char *json_id_prefix(const tal_t *ctx, const struct command *cmd)
276+
/* Prefix is usually a cmd->id */
277+
static const char *json_id(const tal_t *ctx, struct plugin *plugin,
278+
const char *method, const char *prefix)
277279
{
280+
const char *rawid;
281+
int rawidlen;
282+
278283
/* Strip quotes! */
279-
if (strstarts(cmd->id, "\"")) {
280-
assert(strlen(cmd->id) >= 2);
281-
assert(strends(cmd->id, "\""));
282-
return tal_fmt(ctx, "%.*s/",
283-
(int)strlen(cmd->id) - 2, cmd->id + 1);
284+
if (strstarts(prefix, "\"")) {
285+
assert(strlen(prefix) >= 2);
286+
assert(strends(prefix, "\""));
287+
rawid = prefix + 1;
288+
rawidlen = strlen(prefix) - 2;
289+
} else {
290+
rawid = prefix;
291+
rawidlen = strlen(prefix);
284292
}
285-
return tal_fmt(ctx, "%s/", cmd->id);
286-
}
287293

288-
static const char *append_json_id(const tal_t *ctx,
289-
struct plugin *plugin,
290-
const char *method,
291-
const char *prefix)
292-
{
293-
return tal_fmt(ctx, "\"%s%s:%s#%"PRIu64"\"",
294-
prefix, plugin->id, method, plugin->next_outreq_id++);
294+
return tal_fmt(ctx, "\"%.*s%s:%s#%"PRIu64"\"",
295+
rawidlen, rawid, plugin->id, method, plugin->next_outreq_id++);
295296
}
296297

297298
static void destroy_out_req(struct out_req *out_req, struct plugin *plugin)
@@ -320,7 +321,7 @@ jsonrpc_request_start_(struct command *cmd,
320321

321322
assert(cmd);
322323
out = tal(cmd, struct out_req);
323-
out->id = append_json_id(out, cmd->plugin, method, id_prefix);
324+
out->id = json_id(out, cmd->plugin, method, id_prefix ? id_prefix : cmd->id);
324325
out->cmd = cmd;
325326
out->cb = cb;
326327
out->errcb = errcb;
@@ -704,7 +705,7 @@ static const jsmntok_t *sync_req(const tal_t *ctx,
704705
const jsmntok_t *contents;
705706
int reqlen;
706707
struct json_out *jout = json_out_new(tmpctx);
707-
const char *id = append_json_id(tmpctx, plugin, method, "init/");
708+
const char *id = json_id(tmpctx, plugin, "init/", method);
708709

709710
json_out_start(jout, NULL, '{');
710711
json_out_addstr(jout, "jsonrpc", "2.0");

plugins/libplugin.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,10 @@ struct out_req *jsonrpc_request_start_(struct command *cmd,
123123
const jsmntok_t *result,
124124
void *arg),
125125
void *arg)
126-
NON_NULL_ARGS(1, 2, 3, 5);
126+
NON_NULL_ARGS(1, 2, 5);
127127

128-
/* This variant has callbacks received whole obj, not "result" or
129-
* "error" members. */
130-
#define jsonrpc_request_start(cmd, method, cb, errcb, arg) \
131-
jsonrpc_request_start_((cmd), (method), \
132-
json_id_prefix(tmpctx, (cmd)), NULL, \
128+
#define jsonrpc_request_start(cmd, method, cb, errcb, arg) \
129+
jsonrpc_request_start_((cmd), (method), NULL, NULL, \
133130
typesafe_cb_preargs(struct command_result *, void *, \
134131
(cb), (arg), \
135132
struct command *command, \
@@ -143,8 +140,7 @@ struct out_req *jsonrpc_request_start_(struct command *cmd,
143140
(arg))
144141

145142
#define jsonrpc_request_with_filter_start(cmd, method, filter, cb, errcb, arg) \
146-
jsonrpc_request_start_((cmd), (method), \
147-
json_id_prefix(tmpctx, (cmd)), (filter), \
143+
jsonrpc_request_start_((cmd), (method), NULL, (filter), \
148144
typesafe_cb_preargs(struct command_result *, void *, \
149145
(cb), (arg), \
150146
struct command *command, \
@@ -160,7 +156,7 @@ struct out_req *jsonrpc_request_start_(struct command *cmd,
160156
/* This variant has callbacks received whole obj, not "result" or
161157
* "error" members. It also doesn't start params{}. */
162158
#define jsonrpc_request_whole_object_start(cmd, method, id_prefix, cb, arg) \
163-
jsonrpc_request_start_((cmd), (method), (id_prefix), NULL, \
159+
jsonrpc_request_start_((cmd), (method), (id_prefix), NULL, \
164160
typesafe_cb_preargs(struct command_result *, void *, \
165161
(cb), (arg), \
166162
struct command *command, \
@@ -641,9 +637,6 @@ struct createonion_response *json_to_createonion_response(const tal_t *ctx,
641637
struct route_hop *json_to_route(const tal_t *ctx, const char *buffer,
642638
const jsmntok_t *toks);
643639

644-
/* Create a prefix (ending in /) for this cmd_id, if any. */
645-
const char *json_id_prefix(const tal_t *ctx, const struct command *cmd);
646-
647640
void plugin_set_memleak_handler(struct plugin *plugin,
648641
void (*mark_mem)(struct plugin *plugin,
649642
struct htable *memtable));

plugins/test/run-route-calc.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNN
161161
const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
162162
const char *label UNNEEDED)
163163
{ fprintf(stderr, "json_get_member called!\n"); abort(); }
164-
/* Generated stub for json_id_prefix */
165-
const char *json_id_prefix(const tal_t *ctx UNNEEDED, const struct command *cmd UNNEEDED)
166-
{ fprintf(stderr, "json_id_prefix called!\n"); abort(); }
167164
/* Generated stub for json_next */
168165
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
169166
{ fprintf(stderr, "json_next called!\n"); abort(); }

plugins/test/run-route-overlong.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNN
158158
const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED,
159159
const char *label UNNEEDED)
160160
{ fprintf(stderr, "json_get_member called!\n"); abort(); }
161-
/* Generated stub for json_id_prefix */
162-
const char *json_id_prefix(const tal_t *ctx UNNEEDED, const struct command *cmd UNNEEDED)
163-
{ fprintf(stderr, "json_id_prefix called!\n"); abort(); }
164161
/* Generated stub for json_next */
165162
const jsmntok_t *json_next(const jsmntok_t *tok UNNEEDED)
166163
{ fprintf(stderr, "json_next called!\n"); abort(); }

0 commit comments

Comments
 (0)