Skip to content

Commit

Permalink
reckless-rpc: catch failed json parsing of reckless output
Browse files Browse the repository at this point in the history
  • Loading branch information
endothermicdev committed Aug 7, 2024
1 parent ffb8482 commit 8628794
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions plugins/recklessrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,36 @@ static struct command_result *reckless_result(struct io_conn *conn,
reckless->process_failed);
return command_finished(reckless->cmd, response);
}
response = jsonrpc_stream_success(reckless->cmd);
json_array_start(response, "result");
const jsmntok_t *results, *result, *logs, *log;
size_t i;
jsmn_parser parser;
jsmntok_t *toks;
toks = tal_arr(reckless, jsmntok_t, 500);
toks = tal_arr(reckless, jsmntok_t, 5000);
jsmn_init(&parser);
if (jsmn_parse(&parser, reckless->stdoutbuf,
strlen(reckless->stdoutbuf), toks, tal_count(toks)) <= 0) {
plugin_log(plugin, LOG_DBG, "need more json tokens");
assert(false);
int res;
res = jsmn_parse(&parser, reckless->stdoutbuf,
strlen(reckless->stdoutbuf), toks, tal_count(toks));
const char *err;
if (res == JSMN_ERROR_INVAL)
err = tal_fmt(tmpctx, "reckless returned invalid character in json "
"output");
else if (res == JSMN_ERROR_PART)
err = tal_fmt(tmpctx, "reckless returned partial output");
else if (res == JSMN_ERROR_NOMEM )
err = tal_fmt(tmpctx, "insufficient tokens to parse "
"reckless output.");
else
err = NULL;

if (err) {
plugin_log(plugin, LOG_UNUSUAL, "failed to parse json: %s", err);
response = jsonrpc_stream_fail(reckless->cmd, PLUGIN_ERROR,
err);
return command_finished(reckless->cmd, response);
}

response = jsonrpc_stream_success(reckless->cmd);
json_array_start(response, "result");
results = json_get_member(reckless->stdoutbuf, toks, "result");
json_for_each_arr(i, result, results) {
json_add_string(response,
Expand Down

0 comments on commit 8628794

Please sign in to comment.