From 8c2a9a6ed60d926c4ddac9655f8ec9203e1495f8 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 17:17:25 +0000 Subject: [PATCH] fix(ffi): bring src/interface/ffi/src/main.zig to the template form The template added a module-level dupeZ helper to main.zig after this repository was cut from it (Allocator.dupeZ was removed in Zig 0.17). The template upgrade added tests/ffi_strings_test.sh, which calls that helper, but did not apply the matching main.zig change, so the test fails to compile with "use of undeclared identifier 'dupeZ'". Proven 3-outcome fails on main for this reason. main.zig differed from the template only by the project name, so the change is the template file with the name filled in. Nothing else changes. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0136P7Zm6f3TYUpUuzGd5KAQ --- src/interface/ffi/src/main.zig | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/interface/ffi/src/main.zig b/src/interface/ffi/src/main.zig index c9fc13dc..caac0226 100644 --- a/src/interface/ffi/src/main.zig +++ b/src/interface/ffi/src/main.zig @@ -25,6 +25,16 @@ fn clearError() void { last_error = null; } +/// Duplicate a byte slice as a NUL-terminated buffer across Zig 0.15/0.16/0.17 +/// (`Allocator.dupeZ` was removed in Zig 0.17). +/// The caller owns the returned slice and must free it with `allocator`. +/// Propagates allocation failure. +fn dupeZ(allocator: std.mem.Allocator, bytes: []const u8) ![:0]u8 { + const buf = try allocator.allocSentinel(u8, bytes.len, 0); + @memcpy(buf[0..bytes.len], bytes); + return buf; +} + //============================================================================== // Core Types (must match src/abi/Types.idr) //============================================================================== @@ -110,7 +120,9 @@ export fn bactrian_process(handle: ?*Handle, input: u32) Result { //============================================================================== /// Get a string result (example) -/// Caller must free the returned string +/// Return a NUL-terminated "Example result" owned by the caller; free it with +/// `bactrian_free_string`. Return null and set the thread-local error for a +/// null or uninitialized handle, or allocation failure. Clear the error on success. export fn bactrian_get_string(handle: ?*Handle) ?[*:0]const u8 { const h = handle orelse { setError("Null handle"); @@ -123,7 +135,7 @@ export fn bactrian_get_string(handle: ?*Handle) ?[*:0]const u8 { } // Example: allocate and return a string - const result = h.allocator.dupeZ(u8, "Example result") catch { + const result = dupeZ(h.allocator, "Example result") catch { setError("Failed to allocate string"); return null; }; @@ -180,14 +192,15 @@ export fn bactrian_process_array( // Error Handling //============================================================================== -/// Get the last error message -/// Returns null if no error +/// Get the last error message. +/// Return an allocated NUL-terminated copy of the current thread's error, or +/// null if no error is set or allocation fails. Leave the stored error unchanged. +/// Caller must free a non-null returned string with `bactrian_free_string`. export fn bactrian_last_error() ?[*:0]const u8 { const err = last_error orelse return null; - // Return C string (static storage, no need to free) const allocator = std.heap.c_allocator; - const c_str = allocator.dupeZ(u8, err) catch return null; + const c_str = dupeZ(allocator, err) catch return null; return c_str.ptr; } @@ -258,6 +271,10 @@ test "lifecycle" { defer bactrian_free(handle); try std.testing.expect(bactrian_is_initialized(handle) == 1); + + const str = bactrian_get_string(handle) orelse return error.StringFailed; + defer bactrian_free_string(str); + try std.testing.expectEqualStrings("Example result", std.mem.span(str)); } test "error handling" { @@ -265,6 +282,7 @@ test "error handling" { try std.testing.expectEqual(Result.null_pointer, result); const err = bactrian_last_error(); + defer bactrian_free_string(err); try std.testing.expect(err != null); }