0bf258
From 7cb160efe19d3dfb8b92629805733ea186b55050 Mon Sep 17 00:00:00 2001
0bf258
From: "Christoph M. Becker" <cmbecker69@gmx.de>
0bf258
Date: Mon, 31 Oct 2022 17:20:23 +0100
0bf258
Subject: [PATCH] Fix #81740: PDO::quote() may return unquoted string
0bf258
0bf258
`sqlite3_snprintf()` expects its first parameter to be `int`; we need
0bf258
to avoid overflow.
0bf258
0bf258
(cherry picked from commit 921b6813da3237a83e908998483f46ae3d8bacba)
0bf258
---
0bf258
 ext/pdo_sqlite/sqlite_driver.c     |  3 +++
0bf258
 ext/pdo_sqlite/tests/bug81740.phpt | 17 +++++++++++++++++
0bf258
 2 files changed, 20 insertions(+)
0bf258
 create mode 100644 ext/pdo_sqlite/tests/bug81740.phpt
0bf258
0bf258
diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c
0bf258
index 0595bd09fe..54f9d05e1e 100644
0bf258
--- a/ext/pdo_sqlite/sqlite_driver.c
0bf258
+++ b/ext/pdo_sqlite/sqlite_driver.c
0bf258
@@ -233,6 +233,9 @@ static char *pdo_sqlite_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t
0bf258
 /* NB: doesn't handle binary strings... use prepared stmts for that */
0bf258
 static int sqlite_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype )
0bf258
 {
0bf258
+	if (unquotedlen > (INT_MAX - 3) / 2) {
0bf258
+		return 0;
0bf258
+	}
0bf258
 	*quoted = safe_emalloc(2, unquotedlen, 3);
0bf258
 	sqlite3_snprintf(2*unquotedlen + 3, *quoted, "'%q'", unquoted);
0bf258
 	*quotedlen = strlen(*quoted);
0bf258
diff --git a/ext/pdo_sqlite/tests/bug81740.phpt b/ext/pdo_sqlite/tests/bug81740.phpt
0bf258
new file mode 100644
0bf258
index 0000000000..99fb07c304
0bf258
--- /dev/null
0bf258
+++ b/ext/pdo_sqlite/tests/bug81740.phpt
0bf258
@@ -0,0 +1,17 @@
0bf258
+--TEST--
0bf258
+Bug #81740 (PDO::quote() may return unquoted string)
0bf258
+--SKIPIF--
0bf258
+
0bf258
+if (!extension_loaded('pdo_sqlite')) print 'skip not loaded';
0bf258
+if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
0bf258
+?>
0bf258
+--INI--
0bf258
+memory_limit=-1
0bf258
+--FILE--
0bf258
+
0bf258
+$pdo = new PDO("sqlite::memory:");
0bf258
+$string = str_repeat("a", 0x80000000);
0bf258
+var_dump($pdo->quote($string));
0bf258
+?>
0bf258
+--EXPECT--
0bf258
+bool(false)