From c5786c5d38f1f7edd986ed4a4d5339e450511df3 Mon Sep 17 00:00:00 2001 From: CoderJoshDK <74162303+CoderJoshDK@users.noreply.github.com> Date: Wed, 24 Sep 2025 08:28:32 -0400 Subject: [PATCH] fix: alloc free off by one --- include/ghostty.h | 1 + src/config/CApi.zig | 3 ++- src/main_c.zig | 7 +++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/ghostty.h b/include/ghostty.h index 7888b380c..a2964c227 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -353,6 +353,7 @@ typedef struct { typedef struct { const char* ptr; uintptr_t len; + uintptr_t cap; } ghostty_string_s; typedef struct { diff --git a/src/config/CApi.zig b/src/config/CApi.zig index bdc59797a..f90b0ca24 100644 --- a/src/config/CApi.zig +++ b/src/config/CApi.zig @@ -130,7 +130,8 @@ export fn ghostty_config_open_path() c.String { return .empty; }; - return .fromSlice(path); + // Capacity is len + 1 due to sentinel + return .fromSlice(path, path.len + 1); } /// Sync with ghostty_diagnostic_s diff --git a/src/main_c.zig b/src/main_c.zig index 9a9bcc6d2..1212e0b07 100644 --- a/src/main_c.zig +++ b/src/main_c.zig @@ -63,16 +63,19 @@ const Info = extern struct { pub const String = extern struct { ptr: ?[*]const u8, len: usize, + cap: usize, pub const empty: String = .{ .ptr = null, .len = 0, + .cap = 0, }; - pub fn fromSlice(slice: []const u8) String { + pub fn fromSlice(slice: []const u8, cap: usize) String { return .{ .ptr = slice.ptr, .len = slice.len, + .cap = cap, }; } }; @@ -129,5 +132,5 @@ pub export fn ghostty_translate(msgid: [*:0]const u8) [*:0]const u8 { /// Free a string allocated by Ghostty. pub export fn ghostty_string_free(str: String) void { - state.alloc.free(str.ptr.?[0..str.len]); + state.alloc.free(str.ptr.?[0..str.cap]); }