Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit 1032020

Browse files
committed
allocate error message in init with sqlite malloc
SQLite attempts to free the error message so it must be allocated with sqlite malloc
1 parent 68c81ce commit 1032020

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/main.zig

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub export fn sqlite3_stanchion_init(
3131
c.sqlite3_api = api;
3232

3333
if (sqliteVersion() < min_sqlite_version) {
34-
err_msg.* = @constCast(@ptrCast("stanchion requires sqlite version >= 3.26.0"));
34+
setErrorMsg(err_msg, "stanchion requires sqlite version >= 3.26.0");
3535
return c.SQLITE_ERROR;
3636
}
3737

@@ -47,7 +47,7 @@ pub export fn sqlite3_stanchion_init(
4747
null,
4848
);
4949
if (res != c.SQLITE_OK) {
50-
err_msg.* = @constCast(@ptrCast("error creating stanchion module"));
50+
setErrorMsg(err_msg, "error creating stanchion module");
5151
return res;
5252
}
5353

@@ -59,7 +59,7 @@ pub export fn sqlite3_stanchion_init(
5959
null,
6060
);
6161
if (res != c.SQLITE_OK) {
62-
err_msg.* = @constCast(@ptrCast("error creating stanchion_segments module"));
62+
setErrorMsg(err_msg, "error creating stanchion_segments module");
6363
return res;
6464
}
6565

@@ -71,9 +71,15 @@ pub export fn sqlite3_stanchion_init(
7171
null,
7272
);
7373
if (res != c.SQLITE_OK) {
74-
err_msg.* = @constCast(@ptrCast("error creating stanchion_segments module"));
74+
setErrorMsg(err_msg, "error creating stanchion_segment_info module");
7575
return res;
7676
}
7777

7878
return c.SQLITE_OK;
7979
}
80+
81+
fn setErrorMsg(err_msg: [*c][*c]u8, msg_text: []const u8) void {
82+
const msg_buf: [*c]u8 = @ptrCast(c.sqlite3_malloc(@intCast(msg_text.len)));
83+
@memcpy(msg_buf[0..msg_text.len], msg_text);
84+
err_msg.* = msg_buf;
85+
}

0 commit comments

Comments
 (0)