Skip to content

Commit cc125f2

Browse files
committed
Implement #72653: SQLite should allow opening with empty filename
From the [sqlite3_open](https://www.sqlite.org/c3ref/open.html) docs: | If the filename is an empty string, then a private, temporary on-disk | database will be created. This private database will be automatically | deleted as soon as the database connection is closed. We make that facility available to userland. While we're at it, we also do some minor optimizations, remove the unnecessary check for NUL characters in filename, which is already catered to by ZPP(p), and add a missing `return` in case db_obj isn't initialized.
1 parent cce457c commit cc125f2

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ PHP NEWS
6565
. Fixed bug #72646 (SplFileObject::getCsvControl does not return the escape
6666
character). (cmb)
6767

68+
- SQLite3:
69+
. Implemented FR #72653 (SQLite should allow opening with empty filename).
70+
(cmb)
71+
6872
21 Jul 2016, PHP 5.6.24
6973

7074
- Core:

ext/sqlite3/sqlite3.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,11 @@ PHP_METHOD(sqlite3, open)
118118

119119
if (db_obj->initialised) {
120120
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Already initialised DB Object", 0 TSRMLS_CC);
121-
}
122-
123-
if (strlen(filename) != filename_len) {
124121
return;
125122
}
126-
if (filename_len != sizeof(":memory:")-1 ||
127-
memcmp(filename, ":memory:", sizeof(":memory:")-1) != 0) {
123+
124+
if (filename_len != 0 && (filename_len != sizeof(":memory:")-1 ||
125+
memcmp(filename, ":memory:", sizeof(":memory:")-1) != 0)) {
128126
if (!(fullpath = expand_filepath(filename, NULL TSRMLS_CC))) {
129127
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unable to expand filepath", 0 TSRMLS_CC);
130128
return;
@@ -144,7 +142,8 @@ PHP_METHOD(sqlite3, open)
144142
return;
145143
}
146144
} else {
147-
fullpath = estrdup(filename);
145+
/* filename equals "" or ":memory:" */
146+
fullpath = filename;
148147
}
149148

150149
#if SQLITE_VERSION_NUMBER >= 3005000
@@ -153,7 +152,7 @@ PHP_METHOD(sqlite3, open)
153152
if (sqlite3_open(fullpath, &(db_obj->db)) != SQLITE_OK) {
154153
#endif
155154
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
156-
if (fullpath) {
155+
if (fullpath != filename) {
157156
efree(fullpath);
158157
}
159158
return;
@@ -178,7 +177,7 @@ PHP_METHOD(sqlite3, open)
178177
sqlite3_set_authorizer(db_obj->db, php_sqlite3_authorizer, NULL);
179178
}
180179

181-
if (fullpath) {
180+
if (fullpath != filename) {
182181
efree(fullpath);
183182
}
184183
}

ext/sqlite3/tests/sqlite3_open_empty_string.phpt

+2-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ Thijs Feryn <[email protected]>
77
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
88
--FILE--
99
<?php
10-
try{
11-
$db = new SQLite3('');
12-
} catch(Exception $e) {
13-
echo $e->getMessage().PHP_EOL;
14-
}
10+
$db = new SQLite3('');
1511
echo "Done\n";
1612
?>
17-
--EXPECTF--
18-
Unable to expand filepath
13+
--EXPECT--
1914
Done

0 commit comments

Comments
 (0)