From 1032020297a5c87e6428c3c00e36f2cc670a3a09 Mon Sep 17 00:00:00 2001 From: Dan Gallagher Date: Wed, 24 Jan 2024 16:54:15 -0500 Subject: [PATCH] allocate error message in init with sqlite malloc SQLite attempts to free the error message so it must be allocated with sqlite malloc --- src/main.zig | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main.zig b/src/main.zig index 29f8cbc..05957db 100644 --- a/src/main.zig +++ b/src/main.zig @@ -31,7 +31,7 @@ pub export fn sqlite3_stanchion_init( c.sqlite3_api = api; if (sqliteVersion() < min_sqlite_version) { - err_msg.* = @constCast(@ptrCast("stanchion requires sqlite version >= 3.26.0")); + setErrorMsg(err_msg, "stanchion requires sqlite version >= 3.26.0"); return c.SQLITE_ERROR; } @@ -47,7 +47,7 @@ pub export fn sqlite3_stanchion_init( null, ); if (res != c.SQLITE_OK) { - err_msg.* = @constCast(@ptrCast("error creating stanchion module")); + setErrorMsg(err_msg, "error creating stanchion module"); return res; } @@ -59,7 +59,7 @@ pub export fn sqlite3_stanchion_init( null, ); if (res != c.SQLITE_OK) { - err_msg.* = @constCast(@ptrCast("error creating stanchion_segments module")); + setErrorMsg(err_msg, "error creating stanchion_segments module"); return res; } @@ -71,9 +71,15 @@ pub export fn sqlite3_stanchion_init( null, ); if (res != c.SQLITE_OK) { - err_msg.* = @constCast(@ptrCast("error creating stanchion_segments module")); + setErrorMsg(err_msg, "error creating stanchion_segment_info module"); return res; } return c.SQLITE_OK; } + +fn setErrorMsg(err_msg: [*c][*c]u8, msg_text: []const u8) void { + const msg_buf: [*c]u8 = @ptrCast(c.sqlite3_malloc(@intCast(msg_text.len))); + @memcpy(msg_buf[0..msg_text.len], msg_text); + err_msg.* = msg_buf; +}