Skip to content

ext/dba: end LMDB cursor iteration on write/delete#21979

Open
iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/dba-lmdb-cursor-write-txn
Open

ext/dba: end LMDB cursor iteration on write/delete#21979
iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/dba-lmdb-cursor-write-txn

Conversation

@iliaal
Copy link
Copy Markdown
Contributor

@iliaal iliaal commented May 8, 2026

dba_insert/replace/delete on an LMDB handler started a write txn into LMDB_IT(txn) while dba_firstkey's cursor was still bound to the read txn in that slot, so the next dba_nextkey renewed a freed handle. Close the cursor and abort the read txn before starting the write txn, and bail out of dba_nextkey when no cursor is open.

Comment on lines +17 to +23
$db = dba_open($db_file, 'cl', 'lmdb');
foreach (['a' => '1', 'b' => '2', 'c' => '3'] as $k => $v) {
dba_replace($k, $v, $db);
}

var_dump(dba_firstkey($db));
var_dump(dba_nextkey($db));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you have add a test, ideally in a different file, which calls dba_nextkey() immediately after a dob_open?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added dba_lmdb_nextkey_without_firstkey.phpt covering dba_nextkey() immediately after open (and again after a dba_replace()), both return false.

Copy link
Copy Markdown
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable

dba_insert/replace/delete on an LMDB handler started a write txn into
LMDB_IT(txn) while dba_firstkey's cursor was still bound to the read txn
in that slot, so the next dba_nextkey renewed a freed handle. Close the
cursor and abort the read txn before starting the write txn, and bail
out of dba_nextkey when no cursor is open.
@iliaal iliaal force-pushed the fix/dba-lmdb-cursor-write-txn branch from f79c75c to f119935 Compare May 11, 2026 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants