Skip to content

Commit 4a7c554

Browse files
authored
Fix segfault on multiple connection closing (#162)
Problem: - As described in #161, closing a connection more than once leads to a SIGSEGV termination Solution: - check if conn->db is already NULL while closing and return early + test case for double connection closing
1 parent 096e0c2 commit 4a7c554

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

c_src/sqlite3_nif.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ exqlite_close(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
154154
return make_error_tuple(env, "invalid_connection");
155155
}
156156

157+
// DB is already closed, nothing to do here
158+
if (conn->db == NULL) {
159+
return make_atom(env, "ok");
160+
}
161+
157162
int autocommit = sqlite3_get_autocommit(conn->db);
158163
if (autocommit == 0) {
159164
rc = sqlite3_exec(conn->db, "ROLLBACK;", NULL, NULL, NULL);

test/exqlite/sqlite3_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ defmodule Exqlite.Sqlite3Test do
2525
{:ok, conn} = Sqlite3.open(":memory:")
2626
:ok = Sqlite3.close(conn)
2727
end
28+
29+
test "closing a database multiple times works properly" do
30+
{:ok, conn} = Sqlite3.open(":memory:")
31+
:ok = Sqlite3.close(conn)
32+
:ok = Sqlite3.close(conn)
33+
end
2834
end
2935

3036
describe ".execute/2" do

0 commit comments

Comments
 (0)