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