RESOLVED FIXED 237130
SQLiteDatabase::open should return early if journal mode cannot be set
https://bugs.webkit.org/show_bug.cgi?id=237130
Summary SQLiteDatabase::open should return early if journal mode cannot be set
Ben Nham
Reported 2022-02-23 22:36:05 PST
SQLiteDatabase should bail out if opening fails
Attachments
Patch (2.01 KB, patch)
2022-02-23 22:40 PST, Ben Nham
no flags
Patch (9.45 KB, patch)
2022-03-02 16:00 PST, Sihui Liu
no flags
Patch (9.53 KB, patch)
2022-03-03 12:08 PST, Sihui Liu
no flags
Patch for landing (8.36 KB, patch)
2022-03-03 23:19 PST, Sihui Liu
no flags
Ben Nham
Comment 1 2022-02-23 22:40:53 PST
Ben Nham
Comment 2 2022-02-23 22:41:50 PST
Sihui Liu
Comment 3 2022-02-23 23:13:11 PST
Comment on attachment 453076 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=453076&action=review > Source/WebCore/platform/sql/SQLiteDatabase.cpp:145 > + if (!isOpen()) { Why not return earlier? E.g. after sqlite3_open_v2() call above?
Sihui Liu
Comment 4 2022-03-02 16:00:58 PST
Sihui Liu
Comment 5 2022-03-03 12:08:15 PST
Darin Adler
Comment 6 2022-03-03 16:34:08 PST
Comment on attachment 453774 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=453774&action=review > Source/WebCore/platform/sql/SQLiteDatabase.cpp:132 > + int result = SQLITE_OK; Just because there are braces below doesn’t mean we need to initialize this here. I suppose it’s OK. > Source/WebCore/platform/sql/SQLiteDatabase.h:102 > + int checkpoint(CheckpointMode); Why add this return value? No caller is checking it. Is it for future use? > Source/WebCore/platform/sql/SQLiteDatabase.h:172 > + int useWALJournalMode(); Seems like returning an error code as an int with no comment or type to disambiguate and say what the return value means is not a great pattern. In this particular case, the caller just needs to know if this succeeded or failed, so a bool for success would work and I think might be better than returning the error code. I suppose it’s not a *disaster* to return an SQLite error code as an int, but how would I know that’s what this function returns? Elsewhere we use named types whenever possible for error codes, or enums or special-purpose objects. I think booleans for success work too.
Sihui Liu
Comment 7 2022-03-03 17:04:12 PST
Comment on attachment 453774 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=453774&action=review >> Source/WebCore/platform/sql/SQLiteDatabase.h:102 >> + int checkpoint(CheckpointMode); > > Why add this return value? No caller is checking it. Is it for future use? I was planned to use the return value to decide if error is lethal and we should close database immediately. Then I decided to just keep existing behavior that ignores the checkpoint error. So this is unused; I will revert this change. >> Source/WebCore/platform/sql/SQLiteDatabase.h:172 >> + int useWALJournalMode(); > > Seems like returning an error code as an int with no comment or type to disambiguate and say what the return value means is not a great pattern. > > In this particular case, the caller just needs to know if this succeeded or failed, so a bool for success would work and I think might be better than returning the error code. > > I suppose it’s not a *disaster* to return an SQLite error code as an int, but how would I know that’s what this function returns? Elsewhere we use named types whenever possible for error codes, or enums or special-purpose objects. I think booleans for success work too. Sure, will use bool instead. We can get the error with sqlite3_errcode.
Ben Nham
Comment 8 2022-03-03 19:40:25 PST
Patch looks reasonable to me. Just for the future, here is what we found: 1. sqlite3_open_v2 can succeed even if you can't open or create the -shm file for whatever reason (e.g. out of disk space or file handles). 2. Executing `PRAGMA journal_mode=wal` seems to fail if you can't open or create the -shm file. But we were ignoring the result of this operation. 3. Then we would try to call sqlite3_wal_checkpoint_v2 on the connection. This actually crashes if you can't open the -shm file. So the workaround in this patch was to add a bailout at (2) rather than crashing at (3).
Sihui Liu
Comment 9 2022-03-03 23:19:41 PST
Created attachment 453817 [details] Patch for landing
EWS
Comment 10 2022-03-04 00:43:59 PST
Committed r290822 (248058@main): <https://commits.webkit.org/248058@main> All reviewed patches have been landed. Closing bug and clearing flags on attachment 453817 [details].
Note You need to log in before you can comment on or make changes to this bug.