fix: alloc free off by one

pull/8886/head
CoderJoshDK 2025-09-24 08:28:32 -04:00
parent 103e7abad5
commit c5786c5d38
No known key found for this signature in database
3 changed files with 8 additions and 3 deletions

View File

@ -353,6 +353,7 @@ typedef struct {
typedef struct {
const char* ptr;
uintptr_t len;
uintptr_t cap;
} ghostty_string_s;
typedef struct {

View File

@ -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

View File

@ -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]);
}