Bug #79293 [Com]: SQLite3Result::fetchArray() may fetch rows after last row
| From: | oliveadams1999 at gmail dot com | Date: | Thu, 25 Feb 2021 11:25:34 +0000 |
| Subject: | Bug #79293 [Com]: SQLite3Result::fetchArray() may fetch rows after last row | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-232386@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79293&edit=1
ID: 79293
Comment by: oliveadams1999 at gmail dot com
Reported by: cmb@php.net
Summary: SQLite3Result::fetchArray() may fetch rows after
last row
Status: Open
Type: Bug
Package: SQLite related
Operating System: *
PHP Version: 7.3Git-2020-02-21 (Git)
Block user comment: N
Private report: N
New Comment:
Previous menu item; g p: Previous man page; g n: Next man SQLite3Result::fetchArray â Fetches a
result row as an associative or Check with SQLite3Result::numColumns()https://www.mypascoconnect.website/ for an empty
result before calling or not a query will return rows (SQLite3 supports multi-statement queries).
Previous Comments:
------------------------------------------------------------------------
[2020-02-24 11:57:28] cmb@php.net
The following pull request has been associated:
Patch Name: [POC] Fix #64531: SQLite3Result::fetchArray runs the query again
On GitHub: https://github.com/php/php-src/pull/5204
Patch: https://github.com/php/php-src/pull/5204.patch
------------------------------------------------------------------------
[2020-02-21 10:31:13] cmb@php.net
Description:
------------
SQLite3Result::fetchArray() returns FALSE if a query result has
been fully fetched; however, calling SQLite3::fetchArray()
afterwards on this result set may allow to traverse it again
without having called SQLite3Result::reset().
From the sqlite3_step() docs[1] (emphasis mine):
| SQLITE_DONE means that the statement has finished executing
| successfully. sqlite3_step() *should* *not* be called again on
| this virtual machine without first calling sqlite3_reset() to
| reset the virtual machine back to its initial state.
So this behavior of SQLite3Result::fetchArray() relies on
something that should not be done, and therefore might trigger
arbitrary behavior. And even if the behavior is reliable, it
would still be confusing, see e.g. parts of the second example of
bug #64531.
[1] <https://www.sqlite.org/c3ref/step.html>
Test script:
---------------
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE foo (bar INT)");
for ($i = 1; $i <= 3; $i++) {
$db->exec("INSERT INTO foo VALUES ($i)");
}
$res = $db->query("SELECT * FROM foo");
while (($row = $res->fetchArray(SQLITE3_ASSOC))) {
var_dump($row);
}
var_dump($res->fetchArray(SQLITE3_ASSOC));
?>
Expected result:
----------------
array(1) {
["bar"]=>
int(1)
}
array(1) {
["bar"]=>
int(2)
}
array(1) {
["bar"]=>
int(3)
}
bool(false)
Actual result:
--------------
array(1) {
["bar"]=>
int(1)
}
array(1) {
["bar"]=>
int(2)
}
array(1) {
["bar"]=>
int(3)
}
array(1) {
["bar"]=>
int(1)
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79293&edit=1