Skip to content

Commit cc3485e

Browse files
reckless-rpc: catch failed json parsing of reckless output
1 parent 47f20c0 commit cc3485e

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

plugins/recklessrpc.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,36 @@ static struct command_result *reckless_result(struct io_conn *conn,
6969
reckless->process_failed);
7070
return command_finished(reckless->cmd, response);
7171
}
72-
response = jsonrpc_stream_success(reckless->cmd);
73-
json_array_start(response, "result");
7472
const jsmntok_t *results, *result, *logs, *log;
7573
size_t i;
7674
jsmn_parser parser;
7775
jsmntok_t *toks;
78-
toks = tal_arr(reckless, jsmntok_t, 500);
76+
toks = tal_arr(reckless, jsmntok_t, 5000);
7977
jsmn_init(&parser);
80-
if (jsmn_parse(&parser, reckless->stdoutbuf,
81-
strlen(reckless->stdoutbuf), toks, tal_count(toks)) <= 0) {
82-
plugin_log(plugin, LOG_DBG, "need more json tokens");
83-
assert(false);
78+
int res;
79+
res = jsmn_parse(&parser, reckless->stdoutbuf,
80+
strlen(reckless->stdoutbuf), toks, tal_count(toks));
81+
const char *err;
82+
if (res == JSMN_ERROR_INVAL)
83+
err = tal_fmt(tmpctx, "reckless returned invalid character in json "
84+
"output");
85+
else if (res == JSMN_ERROR_PART)
86+
err = tal_fmt(tmpctx, "reckless returned partial output");
87+
else if (res == JSMN_ERROR_NOMEM )
88+
err = tal_fmt(tmpctx, "insufficient tokens to parse "
89+
"reckless output.");
90+
else
91+
err = NULL;
92+
93+
if (err) {
94+
plugin_log(plugin, LOG_UNUSUAL, "failed to parse json: %s", err);
95+
response = jsonrpc_stream_fail(reckless->cmd, PLUGIN_ERROR,
96+
err);
97+
return command_finished(reckless->cmd, response);
8498
}
8599

100+
response = jsonrpc_stream_success(reckless->cmd);
101+
json_array_start(response, "result");
86102
results = json_get_member(reckless->stdoutbuf, toks, "result");
87103
json_for_each_arr(i, result, results) {
88104
json_add_string(response,

0 commit comments

Comments
 (0)