fix: panic on extra long plane names

This commit is contained in:
CJ van den Berg 2025-07-03 16:27:24 +02:00
parent c37f6d0b8d
commit 8281f65011
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -11,10 +11,12 @@ const RGB = @import("color").RGB;
const Plane = @This(); const Plane = @This();
const name_buf_len = 128;
window: vaxis.Window, window: vaxis.Window,
row: i32 = 0, row: i32 = 0,
col: i32 = 0, col: i32 = 0,
name_buf: [128]u8, name_buf: [name_buf_len]u8,
name_len: usize, name_len: usize,
cache: GraphemeCache = .{}, cache: GraphemeCache = .{},
style: vaxis.Cell.Style = .{}, style: vaxis.Cell.Style = .{},
@ -27,7 +29,7 @@ pub const Options = struct {
x: usize = 0, x: usize = 0,
rows: usize = 0, rows: usize = 0,
cols: usize = 0, cols: usize = 0,
name: [*:0]const u8, name: []const u8,
flags: option = .none, flags: option = .none,
}; };
@ -44,13 +46,14 @@ pub fn init(nopts: *const Options, parent_: Plane) !Plane {
.height = @as(u16, @intCast(nopts.rows)), .height = @as(u16, @intCast(nopts.rows)),
.border = .{}, .border = .{},
}; };
const len = @min(nopts.name.len, name_buf_len);
var plane: Plane = .{ var plane: Plane = .{
.window = parent_.window.child(opts), .window = parent_.window.child(opts),
.name_buf = undefined, .name_buf = undefined,
.name_len = std.mem.span(nopts.name).len, .name_len = len,
.scrolling = nopts.flags == .VSCROLL, .scrolling = nopts.flags == .VSCROLL,
}; };
@memcpy(plane.name_buf[0..plane.name_len], nopts.name); @memcpy(plane.name_buf[0..len], nopts.name[0..len]);
return plane; return plane;
} }