Skip to content

Fix JPLua SQLite query loop, Lua stack corruption, close UB, and path resolution - #462

Open
pdewilde wants to merge 1 commit into
Razish:masterfrom
pdewilde:fix/luasqlite-bugs
Open

pdewilde wants to merge 1 commit into
Razish:masterfrom
pdewilde:fix/luasqlite-bugs

Conversation

@pdewilde

Copy link
Copy Markdown

Problem

Using the JPLua SQLite binding (game/g_luasqlite.cpp) fails on normal queries due to several bugs:

  1. Inverted sqlite3_step loop condition:
    while ((error = sqlite3_step(stmt)) != SQLITE_ROW && error != SQLITE_DONE)
    sqlite3_step() returns SQLITE_ROW (100) for each result row and SQLITE_DONE (101) when finished. Because the condition checks != SQLITE_ROW, valid SELECT queries exit the loop immediately on the first row and return an empty table. Conversely, if sqlite3_step() returns an error code (neither SQLITE_ROW nor SQLITE_DONE), the loop body executes on an invalid row and can spin indefinitely.
  2. Lua stack corruption inside the column loop:
    lua_settable(L, top) was placed inside for (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-column SELECT.
  3. Premature DB close & double-close UB:
    • SQLite_Query called sqlite3_close(data->handle) on a prepare error without clearing data->handle, leaving the Lua userdata pointing to a closed database handle.
    • SQLite_Close (also registered as __gc) did not set data->handle = NULL, causing a double-close / use-after-free when GC ran after an explicit :Close().
  4. Data truncation & 0-based row indexing:
    • Used 0-based row indices (int row = 0) instead of Lua's standard 1-based sequence indexing (ipairs / #results).
    • Used sqlite3_column_int (truncating 64-bit SQLite integers) and lua_pushstring for SQLITE_BLOB/SQLITE3_TEXT (truncating blobs at embedded NUL bytes).
  5. Hardcoded japlus/ path (flagged by existing FIXME):
    SQLite_Open hardcoded va("japlus/%s", dbpath), ignoring fs_homepath/fs_game and not supporting :memory: databases or rejecting .. path traversal.
  6. Coupled NO_SQL macro:
    There was no way to compile with SQLite enabled when MySQL headers/libs are unavailable (NO_SQL disabled both).

Fix

  • Change the step loop to while ((error = sqlite3_step(stmt)) == SQLITE_ROW), use 1-based Lua row indices, move lua_settable(L, top) outside the column loop, and check error != SQLITE_DONE after the loop.
  • Use sqlite3_column_int64 for SQLITE_INTEGER and lua_pushlstring(..., sqlite3_column_bytes(...)) for SQLITE_BLOB and SQLITE3_TEXT.
  • Keep the connection open on query errors (returning nil, errmsg), guard against queries on closed handles, and null out data->handle in SQLite_Close.
  • Resolve relative DB paths via fs_homepath + fs_game (rejecting .. and absolute paths, creating missing parent directories on POSIX/Emscripten), and pass :memory: straight through to sqlite3_open.
  • Support independent NO_MYSQL and NO_SQLITE defines alongside NO_SQL in bg_lua.cpp, g_luasql.h, g_luamysql.cpp, and g_luasqlite.cpp.

Verification

Tested via JPLua with both :memory: and file-backed SQLite databases: CREATE TABLE, INSERT, multi-column SELECT iterated with ipairs, invalid SQL error handling (verifying the DB handle stays usable afterwards), and explicit :Close() followed by garbage collection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant