Skip to content

Commit

Permalink
Tighten tests, and fix another Postgres segfault.
Browse files Browse the repository at this point in the history
  • Loading branch information
sparked435 committed Mar 4, 2016
1 parent d5b0bfb commit b47c4b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
27 changes: 17 additions & 10 deletions dbd/postgresql/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,24 @@ static int deallocate(statement_t *statement) {
PGresult *result;
ExecStatusType status;

snprintf(command, IDLEN+13, "DEALLOCATE \"%s\"", statement->name);
result = PQexec(statement->postgresql, command);
/*
* It's possible to get here with a closed database handle
* - either by a mistake by the calling Lua program, or by
* garbage collection. Don't die in that case.
*/
if (!statement->postgresql) {
snprintf(command, IDLEN+13, "DEALLOCATE \"%s\"", statement->name);
result = PQexec(statement->postgresql, command);

if (!result)
return 1;
if (!result)
return 1;

status = PQresultStatus(result);
PQclear(result);
status = PQresultStatus(result);
PQclear(result);

if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK)
return 1;
if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK)
return 1;
}

return 0;
}
Expand Down Expand Up @@ -84,8 +91,8 @@ static int statement_close(lua_State *L) {
*/
deallocate(statement);

PQclear(statement->result);
statement->result = NULL;
PQclear(statement->result);
statement->result = NULL;
}

return 0;
Expand Down
3 changes: 3 additions & 0 deletions tests/run_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ local function test_db_close_doesnt_segfault()
assert.has_error(function()
sth:execute()
end)

-- this also shouldn't segfault.
sth:close()

end

Expand Down

0 comments on commit b47c4b8

Please sign in to comment.