From 80250f9b60c984d886fec8867255c7ac6d08a903 Mon Sep 17 00:00:00 2001 From: Peter Neuroth Date: Mon, 23 Jan 2023 16:23:15 +0100 Subject: [PATCH] datastore: Add check for empty key array We need to check if the key parameter is an empty array in `listdatastore` as we do assume an array of at least length 1 in `wallet.c:5306`. Signed-off-by: Peter Neuroth --- lightningd/datastore.c | 6 +++++- tests/test_misc.py | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lightningd/datastore.c b/lightningd/datastore.c index 828f22047..6967505da 100644 --- a/lightningd/datastore.c +++ b/lightningd/datastore.c @@ -66,7 +66,11 @@ static struct command_result *param_list_or_string(struct command *cmd, const jsmntok_t *tok, const char ***str) { - if (tok->type == JSMN_ARRAY) { + if (tok->type == JSMN_ARRAY && tok->size <= 0) { + return command_fail_badparam(cmd, name, + buffer, tok, + "should not be empty"); + } else if (tok->type == JSMN_ARRAY) { size_t i; const jsmntok_t *t; *str = tal_arr(cmd, const char *, tok->size); diff --git a/tests/test_misc.py b/tests/test_misc.py index ca5a47d32..10532241a 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -2796,12 +2796,21 @@ def test_datastore(node_factory): assert l1.rpc.listdatastore() == {'datastore': []} assert l1.rpc.listdatastore('somekey') == {'datastore': []} + # Fail on empty array + with pytest.raises(RpcError, match='should not be empty'): + l1.rpc.listdatastore([]) + # Add entries. somedata = b'somedata'.hex() somedata_expect = {'key': ['somekey'], 'generation': 0, 'hex': somedata, 'string': 'somedata'} + + # We should fail trying to insert into an empty array + with pytest.raises(RpcError, match='should not be empty'): + l1.rpc.datastore(key=[], hex=somedata) + assert l1.rpc.datastore(key='somekey', hex=somedata) == somedata_expect assert l1.rpc.listdatastore() == {'datastore': [somedata_expect]}