On 01/13/2017 06:25 AM, Jens Alfke wrote:
I’ve found a case where incorrect error information gets reported to client C 
code trying to open a SQLite database. (This is with SQLite 3.14 on mac OS 
10.12.)

After the following C code runs (the path here is irrelevant):
        sqlite3 *db;
        int ret = sqlite3_open_v2("/tmp/foo", &db, SQLITE_OPEN_CREATE | 
SQLITE_OPEN_READONLY, NULL);

the value of `ret` is SQLITE_MISUSE, which makes sense because “create” and 
“readonly” is an illegal combination of flags.
However, the value of `db` is NULL, i.e. no database handle was allocated.
This causes problems when the code continues by trying to get more information 
about the error:

        if (ret != SQLITE_OK) {
                int extendedCode = sqlite3_extended_errcode(db);
                const char *message = sqlite3_errmsg(db);
                report_error_to_user(extendedCode, message);
        }

The value of extendedCode will be SQLITE_NOMEM, and message will be “out of 
memory”. This naturally causes the problem to be reported as an out-of-memory 
error. This happened to me today, and I thought it seemed unlikely on my 16GB 
laptop; but it took me a while to dig to the source of the problem, which is 
that the wrong flags were being used.

sqlite3_open_v2 normally allocates a db handle even on error, so that the 
caller can use the handle to get more information about the error, as above. 
The docs say that the db handle will not be allocated if there wasn’t enough 
memory for the allocation. So it appears that sqlite3_errmsg and 
sqlite3_extended_errcode accept a NULL parameter, but assume that it’s NULL 
because there wasn’t enough memory to allocate a database … which is not true 
in this case.

Fair point.

Note that technically speaking, when SQLite returns SQLITE_MISUSE the behavior is actually undefined. Situations in which the program might segfault if the stars were aligned differently. So this is not actually a bug - just a situation that could be made easier to debug.

Dan.






The fix would seem to be to allocate a database handle in the situation here 
where an illegal combination of flags is used.

—Jens
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to