Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Using the JPLua SQLite binding (
game/g_luasqlite.cpp) fails on normal queries due to several bugs:sqlite3_steploop condition:sqlite3_step()returnsSQLITE_ROW(100) for each result row andSQLITE_DONE(101) when finished. Because the condition checks!= SQLITE_ROW, validSELECTqueries exit the loop immediately on the first row and return an empty table. Conversely, ifsqlite3_step()returns an error code (neitherSQLITE_ROWnorSQLITE_DONE), the loop body executes on an invalid row and can spin indefinitely.lua_settable(L, top)was placed insidefor (int i = 0; i < count; i++)instead of after it. On column 0 it pops the row table and row key into the outer result table; on column 1+ it pops unrelated stack slots, corrupting the Lua stack on any multi-columnSELECT.SQLite_Querycalledsqlite3_close(data->handle)on a prepare error without clearingdata->handle, leaving the Lua userdata pointing to a closed database handle.SQLite_Close(also registered as__gc) did not setdata->handle = NULL, causing a double-close / use-after-free when GC ran after an explicit:Close().int row = 0) instead of Lua's standard 1-based sequence indexing (ipairs/#results).sqlite3_column_int(truncating 64-bit SQLite integers) andlua_pushstringforSQLITE_BLOB/SQLITE3_TEXT(truncating blobs at embedded NUL bytes).japlus/path (flagged by existingFIXME):SQLite_Openhardcodedva("japlus/%s", dbpath), ignoringfs_homepath/fs_gameand not supporting:memory:databases or rejecting..path traversal.NO_SQLmacro:There was no way to compile with SQLite enabled when MySQL headers/libs are unavailable (
NO_SQLdisabled both).Fix
while ((error = sqlite3_step(stmt)) == SQLITE_ROW), use 1-based Lua row indices, movelua_settable(L, top)outside the column loop, and checkerror != SQLITE_DONEafter the loop.sqlite3_column_int64forSQLITE_INTEGERandlua_pushlstring(..., sqlite3_column_bytes(...))forSQLITE_BLOBandSQLITE3_TEXT.nil, errmsg), guard against queries on closed handles, and null outdata->handleinSQLite_Close.fs_homepath+fs_game(rejecting..and absolute paths, creating missing parent directories on POSIX/Emscripten), and pass:memory:straight through tosqlite3_open.NO_MYSQLandNO_SQLITEdefines alongsideNO_SQLinbg_lua.cpp,g_luasql.h,g_luamysql.cpp, andg_luasqlite.cpp.Verification
Tested via JPLua with both
:memory:and file-backed SQLite databases:CREATE TABLE,INSERT, multi-columnSELECTiterated withipairs, invalid SQL error handling (verifying the DB handle stays usable afterwards), and explicit:Close()followed by garbage collection.