cli: Add a JSON parsing sanity check before sending request

If some parameter is malformed on the command line we could end up
with a malformed JSON-RPC request, which would then result in very
unhelpful error messages.

Fixes #4238
Changelog-Changed: cli: `lightning-cli` now performs better sanity checks on the JSON-RPC requests it sends.
This commit is contained in:
Christian Decker 2020-12-04 14:33:38 +01:00 committed by Rusty Russell
parent b2a5cf422f
commit ad2f6cdabb

View File

@ -740,6 +740,14 @@ int main(int argc, char *argv[])
tal_append_fmt(&cmd, "] }");
}
toks = json_parse_simple(ctx, cmd, strlen(cmd));
if (toks == NULL)
errx(ERROR_USAGE,
"Some parameters are malformed, cannot create a valid "
"JSON-RPC request: %s",
cmd);
tal_free(toks);
if (!write_all(fd, cmd, strlen(cmd)))
err(ERROR_TALKING_TO_LIGHTNINGD, "Writing command");