Skip to content

ext/sqlite3: fix wrong pointer types passed to the free list comparator.#21483

Open
devnexen wants to merge 4 commits intophp:PHP-8.5from
devnexen:sqlite3_layer_fix
Open

ext/sqlite3: fix wrong pointer types passed to the free list comparator.#21483
devnexen wants to merge 4 commits intophp:PHP-8.5from
devnexen:sqlite3_layer_fix

Conversation

@devnexen
Copy link
Copy Markdown
Member

SQLite3Stmt::close() and SQLite3Result::finalize() passed php_sqlite3_stmt pointer types instead of sqlite3_stmt pointers to zend_llist_del_element, causing the comparator to never match and both methods to silently become no-ops.

Regression introduced in 5eae6d1 ("Don't store the object zval directly").

SQLite3Stmt::close() and SQLite3Result::finalize() passed
php_sqlite3_stmt pointer types instead of sqlite3_stmt pointers
to zend_llist_del_element, causing the comparator to never match
and both methods to silently become no-ops.

Regression introduced in 5eae6d1 ("Don't store the object
zval directly").
@devnexen devnexen marked this pull request as ready for review March 21, 2026 08:31
@devnexen devnexen requested a review from ndossche March 21, 2026 08:32
Copy link
Copy Markdown
Member

@ndossche ndossche left a comment

Choose a reason for hiding this comment

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

Are you sure?

https://github.com/devnexen/php-src/blob/18d287e5b50301dd36171e4f3fb0760b23a95021/ext/sqlite3/sqlite3.c#L2265
shows that ->stmt is dereferenced there, and the initialization shows:
https://github.com/devnexen/php-src/blob/18d287e5b50301dd36171e4f3fb0760b23a95021/ext/sqlite3/sqlite3.c#L2417
i.e. elements of type php_sqlite3_stmt * should be added.

@devnexen
Copy link
Copy Markdown
Member Author

The pb resides in the second argument not the first

static int php_sqlite3_compare_stmt_free(php_sqlite3_stmt **stmt_obj_ptr, sqlite3_stmt *statement )

@devnexen
Copy link
Copy Markdown
Member Author

by the way here an unchanged line which does not need fix.

Copy link
Copy Markdown
Member

@ndossche ndossche left a comment

Choose a reason for hiding this comment

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

Okay but then the patch here is incomplete as well.
The signature of php_sqlite3_compare_stmt_free is wrong then, as well as its contents. Probably other calls to the linked list code should also be adapted.
In fact, looking at devnexen@bc74cff this code might've always been wrong?

The comparator and all call sites now use php_sqlite3_stmt
pointers, matching the type stored in the free list.
Copy link
Copy Markdown
Member

@ndossche ndossche left a comment

Choose a reason for hiding this comment

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

I believe this is right, but would like a second opinion too.

@ndossche
Copy link
Copy Markdown
Member

I also believe this fixes a real long-standing bug that should be backported at a later point after this one is merged in 8.5.

@devnexen devnexen requested a review from Girgias April 4, 2026 18:29
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.

This looks sensible, but maybe @kamil-tekiela or @SakiTakamachi would be better reviewer as they are more familiar with DB extensions.

@kamil-tekiela
Copy link
Copy Markdown
Member

If it fixes a bug, can we get a unit test?

@kamil-tekiela
Copy link
Copy Markdown
Member

The test passes with no issues without this PR

@devnexen
Copy link
Copy Markdown
Member Author

devnexen commented Apr 6, 2026

You're right, the test passes either way. This is a C-level type correctness fix — the comparator was receiving mismatched pointer types (php_sqlite3_stmt ** vs
sqlite3_stmt *), which is undefined behavior even if it happens to work on common platforms. There's no way to reliably trigger the UB from PHP userland, so the test
just exercises the code path but can't demonstrate the actual fix.

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.

4 participants