terminal: handle aliased title updates

setTitle can receive the slice returned by getTitle. Clearing the list retained its allocation, so appending that same slice used memcpy with aliased source and destination ranges and panicked in runtime-safe builds.

Resize the list within its reserved capacity and copy the value forward before writing the sentinel. This supports the complete current value and its subslices without weakening allocation-failure atomicity.
This commit is contained in:
Mitchell Hashimoto
2026-07-09 21:53:38 -07:00
parent a14a619ceb
commit dace6d1c6d

View File

@@ -3627,9 +3627,9 @@ pub fn setTitle(self: *Terminal, t: []const u8) !void {
return error.OutOfMemory;
try self.title.ensureTotalCapacity(self.gpa(), capacity);
self.title.clearRetainingCapacity();
self.title.appendSliceAssumeCapacity(t);
self.title.appendAssumeCapacity(0);
self.title.items.len = capacity;
std.mem.copyForwards(u8, self.title.items[0..t.len], t);
self.title.items[t.len] = 0;
}
/// Returns the title for the terminal, if any. The memory is owned by the
@@ -3651,6 +3651,15 @@ test "Terminal: setTitle preserves a sentinel on allocation failure" {
try testing.expect(t.getTitle() == null);
}
test "Terminal: setTitle accepts its current value" {
var t = try init(testing.allocator, .{ .cols = 5, .rows = 1 });
defer t.deinit(testing.allocator);
try t.setTitle("Ghostty");
try t.setTitle(t.getTitle().?);
try testing.expectEqualStrings("Ghostty", t.getTitle().?);
}
/// Switch to the given screen type (alternate or primary).
///
/// This does NOT handle behaviors such as clearing the screen,