Bug #79293 [Com]: SQLite3Result::fetchArray() may fetch rows after last row
| From: | manuelbrooks50 at gmail dot com | Date: | Mon, 08 Feb 2021 06:20:38 +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-232001@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: manuelbrooks50 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:
Returns a result row as an associatively or numerically indexed array or both. Alternately will
return false if there are no more rows.
The types of the values of the returned array are mapped from SQLite3 types as follows: integers are
mapped to int if they fit into the range PHP_INT_MIN..PHP_INT https://www.mcdvoice.ltd/
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