autocomplete: fix crash on empty cursor result

cur.get() returns None when no key >= the prefix exists (empty DB or
prefix past the last key). Unpacking None raised TypeError. Guard
against it by checking the result before unpacking.

Also, "while i <= 10" collected up to 11 results; changed to "i < 10"
to return at most 10 as intended.

Signed-off-by: Thomas Perrot <thomas.perrot@bootlin.com>
This commit is contained in:
Thomas Perrot 2026-05-05 17:31:32 +02:00 committed by Théo Lebrun
parent e25dc5e6ee
commit 453580252b

View file

@ -68,14 +68,15 @@ class AutocompleteResource:
# In practice this should mean "the key that starts with provided prefix"
# See docs about the default comparison function for B-Tree databases:
# https://docs.oracle.com/cd/E17276_01/html/api_reference/C/dbset_bt_compare.html
key, _ = cur.get(query_bytes, DB_SET_RANGE)
while i <= 10:
result = cur.get(query_bytes, DB_SET_RANGE)
while result is not None and i < 10:
key, _ = result
if key.startswith(query_bytes):
# If found key starts with the prefix, add to response
# and move to the next key
i += 1
response.append(process(key.decode("utf-8")))
key, _ = cur.next()
result = cur.next()
else:
# If found key does not start with the prefix, stop
break