Skip to content

Commit 945a606

Browse files
committed
Close connection after fialed open
This is required per the documentation, otherwise can result in memory leaks due to unfreed resources.
1 parent 3b6982e commit 945a606

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

ext/sqlite3/database.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,13 @@ rb_sqlite3_open_v2(VALUE self, VALUE file, VALUE mode, VALUE zvfs)
153153
NIL_P(zvfs) ? NULL : StringValuePtr(zvfs)
154154
);
155155

156-
CHECK(ctx->db, status);
156+
if (status != SQLITE_OK) {
157+
char *msg = sqlite3_mprintf("%s", sqlite3_errmsg(ctx->db));
158+
sqlite3_close_v2(ctx->db);
159+
ctx->db = NULL;
160+
CHECK_MSG(ctx->db, status, msg);
161+
}
162+
157163
if (flags & SQLITE_OPEN_READONLY) {
158164
ctx->flags |= SQLITE3_RB_DATABASE_READONLY;
159165
}
@@ -941,7 +947,12 @@ rb_sqlite3_open16(VALUE self, VALUE file)
941947
// so we do not ever set SQLITE3_RB_DATABASE_READONLY in ctx->flags
942948
status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db);
943949

944-
CHECK(ctx->db, status)
950+
if (status != SQLITE_OK) {
951+
char *msg = sqlite3_mprintf("%s", sqlite3_errmsg(ctx->db));
952+
sqlite3_close_v2(ctx->db);
953+
ctx->db = NULL;
954+
CHECK_MSG(ctx->db, status, msg);
955+
}
945956

946957
return INT2NUM(status);
947958
}

0 commit comments

Comments
 (0)