Skip to content

Commit

Permalink
Fix a potential memleak in Database.execQuery.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinnegatamante committed Nov 1, 2023
1 parent 4ca3b2d commit 3538a40
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions source/luaDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ static int sqlite_callback(void *data, int argc, char **argv, char **azColName){
lua_State *L = (lua_State*)data;
lua_pushnumber(L, callback_results++);
lua_newtable(L);
for (int i = 0; i < argc; i++){
for (int i = 0; i < argc; i++) {
lua_pushstring(L, azColName[i]);
if (argv[i] != NULL) lua_pushstring(L, argv[i]);
else lua_pushnil(L);
if (argv[i] != NULL)
lua_pushstring(L, argv[i]);
else
lua_pushnil(L);
lua_settable(L, -3);
}
lua_settable(L, -3);
Expand All @@ -57,7 +59,8 @@ static int sqlite_callback(void *data, int argc, char **argv, char **azColName){
static int lua_opendb(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 1) return luaL_error(L, "wrong number of arguments");
if (argc != 1)
return luaL_error(L, "wrong number of arguments");
#endif
const char *file = luaL_checkstring(L, 1);
sqlite3 *db;
Expand All @@ -71,7 +74,8 @@ static int lua_opendb(lua_State *L){
static int lua_closedb(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 1) return luaL_error(L, "wrong number of arguments");
if (argc != 1)
return luaL_error(L, "wrong number of arguments");
#endif
sqlite3 *db = (sqlite3*)luaL_checkinteger(L, 1);
sqlite3_close(db);
Expand All @@ -81,15 +85,15 @@ static int lua_closedb(lua_State *L){
static int lua_query(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 2) return luaL_error(L, "wrong number of arguments");
if (argc != 2)
return luaL_error(L, "wrong number of arguments");
#endif
sqlite3 *db = (sqlite3*)luaL_checkinteger(L, 1);
const char *query = luaL_checkstring(L, 2);
callback_results = 1;
char *zErrMsg = NULL;
lua_newtable(L);
int fd = sqlite3_exec(db, query, sqlite_callback, L, &zErrMsg);
if (fd != SQLITE_OK){
int fd = sqlite3_exec(db, query, sqlite_callback, L, NULL);
if (fd != SQLITE_OK) {
return luaL_error(L, sqlite3_errmsg(db));
}
return 1;
Expand Down

0 comments on commit 3538a40

Please sign in to comment.