diff --git a/ext/pdo_sqlite/sqlite_driver.c b/ext/pdo_sqlite/sqlite_driver.c index c933c34ed1dcd..d9d830c4df849 100644 --- a/ext/pdo_sqlite/sqlite_driver.c +++ b/ext/pdo_sqlite/sqlite_driver.c @@ -737,7 +737,7 @@ static char *make_filename_safe(const char *filename) } return estrdup(filename); } - if (*filename && strcmp(filename, ":memory:")) { + if (*filename && strncmp(filename, ":memory:", sizeof(":memory:")-1) != 0) { char *fullpath = expand_filepath(filename, NULL); if (!fullpath) { diff --git a/ext/pdo_sqlite/tests/gh20076.phpt b/ext/pdo_sqlite/tests/gh20076.phpt new file mode 100644 index 0000000000000..c32fdde985ac9 --- /dev/null +++ b/ext/pdo_sqlite/tests/gh20076.phpt @@ -0,0 +1,13 @@ +--TEST-- +GH-20076 (open_basedir prohibits opening :memory:?cache=shared) +--EXTENSIONS-- +sqlite3 +--INI-- +open_basedir=xx +--FILE-- + +--EXPECT-- +object(PDO)#1 (0) { +} diff --git a/ext/sqlite3/sqlite3.c b/ext/sqlite3/sqlite3.c index 9b3286b70220d..e3a2332f60821 100644 --- a/ext/sqlite3/sqlite3.c +++ b/ext/sqlite3/sqlite3.c @@ -113,7 +113,7 @@ PHP_METHOD(SQLite3, open) RETURN_THROWS(); } - if (filename_len != 0 && (filename_len != sizeof(":memory:")-1 || + if (filename_len != 0 && (filename_len < sizeof(":memory:")-1 || memcmp(filename, ":memory:", sizeof(":memory:")-1) != 0)) { if (!(fullpath = expand_filepath(filename, NULL))) { zend_throw_exception(zend_ce_exception, "Unable to expand filepath", 0); @@ -126,7 +126,7 @@ PHP_METHOD(SQLite3, open) RETURN_THROWS(); } } else { - /* filename equals "" or ":memory:" */ + /* filename equals "" or starts with ":memory:" */ fullpath = filename; } diff --git a/ext/sqlite3/tests/gh20076.phpt b/ext/sqlite3/tests/gh20076.phpt new file mode 100644 index 0000000000000..406d7955debb9 --- /dev/null +++ b/ext/sqlite3/tests/gh20076.phpt @@ -0,0 +1,13 @@ +--TEST-- +GH-20076 (open_basedir prohibits opening :memory:?cache=shared) +--EXTENSIONS-- +sqlite3 +--INI-- +open_basedir=xx +--FILE-- + +--EXPECT-- +object(SQLite3)#1 (0) { +}