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 <pet.v.ne@gmail.com>
This commit is contained in:
Peter Neuroth 2023-01-23 16:23:15 +01:00 committed by Rusty Russell
parent 959678244c
commit 80250f9b60
2 changed files with 14 additions and 1 deletions

View file

@ -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);

View file

@ -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]}