Skip to content

Commit cf2199b

Browse files
committed
Fix GH-22051: report errors from SQLite3Result reset and finalize
SQLite3Stmt::reset() reports a failed sqlite3_reset() through php_sqlite3_error(), but SQLite3Result::reset() returned false silently and SQLite3Result::finalize() ignored the result. Both now surface the underlying SQLite error the same way; finalize() keeps its : true return type and reports without altering the return value. Fixes GH-22051
1 parent 278137c commit cf2199b

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ PHP NEWS
230230

231231
- Sqlite3:
232232
. Fix NUL byte truncation in sqlite3 TEXT column handling. (ndossche)
233+
. Fixed bug GH-22051 (Inconsistent error reporting in SQLite3Result::reset()
234+
and SQLite3Result::finalize()). (iliaal)
233235

234236
- Standard:
235237
. Fixed bug GH-19926 (reset internal pointer earlier while splicing array

ext/sqlite3/sqlite3.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,7 @@ PHP_METHOD(SQLite3Result, reset)
21252125
sqlite3result_clear_column_names_cache(result_obj);
21262126

21272127
if (sqlite3_reset(result_obj->stmt_obj->stmt) != SQLITE_OK) {
2128+
php_sqlite3_error(result_obj->db_obj, sqlite3_errcode(sqlite3_db_handle(result_obj->stmt_obj->stmt)), "Unable to reset statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
21282129
RETURN_FALSE;
21292130
}
21302131

@@ -2150,7 +2151,9 @@ PHP_METHOD(SQLite3Result, finalize)
21502151
zend_llist_del_element(&(result_obj->db_obj->free_list), result_obj->stmt_obj,
21512152
(int (*)(void *, void *)) php_sqlite3_compare_stmt_free);
21522153
} else {
2153-
sqlite3_reset(result_obj->stmt_obj->stmt);
2154+
if (sqlite3_reset(result_obj->stmt_obj->stmt) != SQLITE_OK) {
2155+
php_sqlite3_error(result_obj->db_obj, sqlite3_errcode(sqlite3_db_handle(result_obj->stmt_obj->stmt)), "Unable to reset statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
2156+
}
21542157
}
21552158

21562159
RETURN_TRUE;

ext/sqlite3/tests/gh22051.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
GH-22051 (SQLite3Result::reset()/finalize() report the error on failure)
3+
--EXTENSIONS--
4+
sqlite3
5+
--FILE--
6+
<?php
7+
$db = new SQLite3(':memory:');
8+
9+
// A query whose second step overflows leaves the statement in an error state.
10+
$sql = "SELECT abs(x) FROM (SELECT 1 AS x UNION ALL SELECT -9223372036854775807 - 1)";
11+
12+
echo "--- SQLite3Result::reset() ---\n";
13+
$result = $db->query($sql);
14+
var_dump($result->fetchArray(SQLITE3_NUM));
15+
var_dump(@$result->fetchArray(SQLITE3_NUM));
16+
var_dump($result->reset());
17+
18+
echo "--- SQLite3Result::finalize() ---\n";
19+
$stmt = $db->prepare($sql);
20+
$result = $stmt->execute();
21+
var_dump($result->fetchArray(SQLITE3_NUM));
22+
var_dump(@$result->fetchArray(SQLITE3_NUM));
23+
var_dump($result->finalize());
24+
?>
25+
--EXPECTF--
26+
--- SQLite3Result::reset() ---
27+
array(1) {
28+
[0]=>
29+
int(1)
30+
}
31+
bool(false)
32+
33+
Warning: SQLite3Result::reset(): Unable to reset statement: integer overflow in %s on line %d
34+
bool(false)
35+
--- SQLite3Result::finalize() ---
36+
array(1) {
37+
[0]=>
38+
int(1)
39+
}
40+
bool(false)
41+
42+
Warning: SQLite3Result::finalize(): Unable to reset statement: integer overflow in %s on line %d
43+
bool(true)

0 commit comments

Comments
 (0)