-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement #72653: SQLite should allow opening with empty filename #2030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
The failing checks are unrelated to this PR. |
@@ -117,7 +117,10 @@ PHP_METHOD(sqlite3, open) | |||
if (strlen(filename) != filename_len) { | |||
return; | |||
} | |||
if (filename_len != sizeof(":memory:")-1 || | |||
if (filename_len == 0) { | |||
fullpath = emalloc(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not fullpath = NULL here ?
Would work, and prevent a one byte allocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that the emalloc(1) is silly, but I'm not sure that fullpath = NULL
would work (it does now, but that might be an implementation detail). The SQLite3 docs say "empty filename".
What about using a stack allocated empty string instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not, but you'll have to track that to be sure not to free it.
Otherwise : change the whole code to use a stack allocated buffer instead of a heap allocated one ;-)
Yep, OK to merge that in 5.6 and 7.0 once comments are addressed |
The issue with emalloc(1) should be solved now. I can "merge" later to PHP 5.6+. |
Great 👍 |
Imho empty_string is an unnecessary microoptimization, it just makes the code dirty. |
If we're going down that route lets make it generic and cover :memory: as well, by setting fullpath = filename and checking for that. |
Okay, so we either want only the first or all three commits. |
@@ -117,8 +117,8 @@ PHP_METHOD(sqlite3, open) | |||
if (strlen(filename) != filename_len) { | |||
return; | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this if
be removed as were ZPP'ing for p
(not s
) anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say yes, it can be removed.
Also, why not use a "P" , to be given a zend_string ? Just a small refactor, zend_string should really be prefered over C strings as much as possible.
The comparaisons would then use zend_string_equals_literal().
I can patch it if you want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, why not use a "P" , to be given a zend_string ?
AIUI, that would be PHP 7 only.
I can patch it if you want.
Yes, go for it. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate PR then, as it will obviously be PHP 7 only ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll commit this one first to PHP 5.6+.
@@ -114,11 +114,8 @@ PHP_METHOD(sqlite3, open) | |||
zend_throw_exception(zend_ce_exception, "Already initialised DB Object", 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unrelated to this PR, but there's a return
missing here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I'll fix it as well.
Comment on behalf of cmb at php.net: "Merged" with cc125f2. |
From the sqlite3_open 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.