From df0fdae43720a73930d230f459722d685970bfc2 Mon Sep 17 00:00:00 2001 From: Tomasz Kowalczyk Date: Thu, 26 Nov 2026 22:35:05 +0100 Subject: [PATCH 1/2] Fix GH-25809: SQLite3::backup() returns true when the backup fails When sqlite3_backup_init() fails, SQLite stores the error in the destination connection, and a failed backup step leaves its message there too. SQLite3::backup() read both from the source connection, so it returned true for a nonexistent database name and reported "not an error" for other failures. Read them from the destination connection. --- NEWS | 4 ++++ ext/sqlite3/sqlite3.c | 4 ++-- ext/sqlite3/tests/gh25809.phpt | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 ext/sqlite3/tests/gh25809.phpt diff --git a/NEWS b/NEWS index e42393462d83..c4985bb9fcb2 100644 --- a/NEWS +++ b/NEWS @@ -230,6 +230,10 @@ PHP NEWS . Fixed sodium_add() and sodium_increment() retaining stale string hashes and UTF-8 validity flags. (lacatoire) +- SQLite: + . Fixed bug GH-25809 (SQLite3::backup() returns true when the backup fails). + (Tomasz Kowalczyk) + - Standard: . Fixed bug GH-25474 (version_compare() treats large numeric parts as equal on 64-bit Windows). (lazerg, timwolla) diff --git a/ext/sqlite3/sqlite3.c b/ext/sqlite3/sqlite3.c index 19a226c1dca9..4d2c74891c9a 100644 --- a/ext/sqlite3/sqlite3.c +++ b/ext/sqlite3/sqlite3.c @@ -1442,7 +1442,7 @@ PHP_METHOD(SQLite3, backup) rc = sqlite3_backup_finish(dbBackup); } else { - rc = sqlite3_errcode(source_obj->db); + rc = sqlite3_errcode(destination_obj->db); } if (rc != SQLITE_OK) { @@ -1453,7 +1453,7 @@ PHP_METHOD(SQLite3, backup) php_sqlite3_error(source_obj, rc, "Backup failed: source database is locked"); } else { - php_sqlite3_error(source_obj, rc, "Backup failed: %s", sqlite3_errmsg(source_obj->db)); + php_sqlite3_error(source_obj, rc, "Backup failed: %s", sqlite3_errmsg(destination_obj->db)); } RETURN_FALSE; } diff --git a/ext/sqlite3/tests/gh25809.phpt b/ext/sqlite3/tests/gh25809.phpt new file mode 100644 index 000000000000..d54f0744b747 --- /dev/null +++ b/ext/sqlite3/tests/gh25809.phpt @@ -0,0 +1,36 @@ +--TEST-- +GH-25809 (SQLite3::backup() returns true when the backup fails) +--EXTENSIONS-- +sqlite3 +--FILE-- +exec('CREATE TABLE t (a); INSERT INTO t VALUES (1)'); +$destination = new SQLite3(':memory:'); + +var_dump($source->backup($destination, 'main', 'nonexistent')); +var_dump($source->backup($destination, 'nonexistent', 'main')); + +$file = __DIR__ . '/gh25809.db'; +@unlink($file); +$writer = new SQLite3($file); +$writer->exec('CREATE TABLE z (a)'); +$writer->close(); +$readonly = new SQLite3($file, SQLITE3_OPEN_READONLY); +var_dump($source->backup($readonly)); + +?> +--CLEAN-- + +--EXPECTF-- +Warning: SQLite3::backup(): Backup failed: unknown database nonexistent in %s on line %d +bool(false) + +Warning: SQLite3::backup(): Backup failed: unknown database nonexistent in %s on line %d +bool(false) + +Warning: SQLite3::backup(): Backup failed: attempt to write a readonly database in %s on line %d +bool(false) From 00ce2502c7ae8f84fee9c21f5f24e4a103b5cb5a Mon Sep 17 00:00:00 2001 From: Tomasz Kowalczyk Date: Fri, 27 Nov 2026 12:00:19 +0100 Subject: [PATCH 2/2] Drop duplicate unlink() from the test --- ext/sqlite3/tests/gh25809.phpt | 1 - 1 file changed, 1 deletion(-) diff --git a/ext/sqlite3/tests/gh25809.phpt b/ext/sqlite3/tests/gh25809.phpt index d54f0744b747..33f4ad12279c 100644 --- a/ext/sqlite3/tests/gh25809.phpt +++ b/ext/sqlite3/tests/gh25809.phpt @@ -13,7 +13,6 @@ var_dump($source->backup($destination, 'main', 'nonexistent')); var_dump($source->backup($destination, 'nonexistent', 'main')); $file = __DIR__ . '/gh25809.db'; -@unlink($file); $writer = new SQLite3($file); $writer->exec('CREATE TABLE z (a)'); $writer->close();