Skip to content

Commit 9f4ef68

Browse files
cmb69adoy
authored andcommitted
Fix #81740: PDO::quote() may return unquoted string
`sqlite3_snprintf()` expects its first parameter to be `int`; we need to avoid overflow.
1 parent 52a891a commit 9f4ef68

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ static zend_string *pdo_sqlite_last_insert_id(pdo_dbh_t *dbh, const zend_string
226226
/* NB: doesn't handle binary strings... use prepared stmts for that */
227227
static zend_string* sqlite_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
228228
{
229+
if (unquotedlen > (INT_MAX - 3) / 2) {
230+
return 0;
231+
}
229232
char *quoted = safe_emalloc(2, ZSTR_LEN(unquoted), 3);
230233
/* TODO use %Q format? */
231234
sqlite3_snprintf(2*ZSTR_LEN(unquoted) + 3, quoted, "'%q'", ZSTR_VAL(unquoted));

ext/pdo_sqlite/tests/bug81740.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #81740 (PDO::quote() may return unquoted string)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('pdo_sqlite')) print 'skip not loaded';
6+
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
7+
?>
8+
--INI--
9+
memory_limit=-1
10+
--FILE--
11+
<?php
12+
$pdo = new PDO("sqlite::memory:");
13+
$string = str_repeat("a", 0x80000000);
14+
var_dump($pdo->quote($string));
15+
?>
16+
--EXPECT--
17+
bool(false)

0 commit comments

Comments
 (0)