lv_micropython/tests/extmod/btree_closed.py
Michael Vornovitsky 6db91dfefb extmod/modbtree: Add checks for already-closed database.
Fixes use-after-free when accessing the database after it is closed with
`btree_close`.  `btree_close` always succeeds when called with an
already-closed database.

The new test checks that operations that access the underlying database
(get, set, flush, seq) fail with a `ValueError` when the btree is already
closed.  It also checks that closing and printing the btree succeed when
the btree is already closed.

Fixes issue #12543.

Signed-off-by: Michael Vornovitsky <michaelvornovitskiy@outlook.com>
2024-07-22 10:42:29 +10:00

40 lines
602 B
Python

try:
import btree
import io
except ImportError:
print("SKIP")
raise SystemExit
f = io.BytesIO()
db = btree.open(f)
db[b"foo"] = b"42"
db.close()
# Accessing an already-closed database should fail.
try:
print(db[b"foo"])
except ValueError:
print("ValueError")
try:
db[b"bar"] = b"43"
except ValueError:
print("ValueError")
try:
db.flush()
except ValueError:
print("ValueError")
try:
for k, v in db.items():
pass
except ValueError:
print("ValueError")
# Closing and printing an already-closed database should not fail.
db.close()
print(db)