Skip to content

Commit

Permalink
datastore: Add check for empty key array
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nepet authored and ddustin committed Apr 11, 2023
1 parent 6e2658c commit 3489445
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lightningd/datastore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand Down

0 comments on commit 3489445

Please sign in to comment.