From 84950136c96a7e22e2dbbd29da74e1ffd1d102bb Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Fri, 10 Jul 2026 00:15:07 +0200 Subject: [PATCH] positionWidgets: Less integer overflows possible Signed-off-by: AnErrupTion --- ly-ui/src/TerminalBuffer.zig | 3 ++- src/animations/Lua.zig | 4 +++- src/main.zig | 22 +++++++++++++++------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/ly-ui/src/TerminalBuffer.zig b/ly-ui/src/TerminalBuffer.zig index 7ead34f..4881a2e 100644 --- a/ly-ui/src/TerminalBuffer.zig +++ b/ly-ui/src/TerminalBuffer.zig @@ -269,7 +269,8 @@ pub fn runEventLoop( } } - try TerminalBuffer.presentBuffer(); + // We don't care about present errors here + TerminalBuffer.presentBuffer() catch {}; } if (inactivity_event_fn) |inactivity_fn| { diff --git a/src/animations/Lua.zig b/src/animations/Lua.zig index d212e03..2d2956f 100644 --- a/src/animations/Lua.zig +++ b/src/animations/Lua.zig @@ -129,8 +129,10 @@ fn draw(self: *Lua) void { cell.put(x, y) catch {}; if (self.lua_str) |str| for (str, 0..) |c, i| { + const dwidth = @divFloor(self.width, 2); + const dlen = @divFloor(str.len, 2); Cell.init(c, 0x00FFFFFF, 0).put( - @divFloor(self.width, 2) - @divFloor(str.len, 2) + i, + (if (dlen > dwidth) 0 else dwidth - dlen) + i, self.margin + 5, ) catch {}; }; diff --git a/src/main.zig b/src/main.zig index 85ababf..382ace4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2244,26 +2244,34 @@ fn positionWidgets(ptr: *anyopaque) !void { bb_width = @max(bb_width, clock_text_len); } - const max_v_position: f32 = @floatFromInt(state.buffer.height - bb_height - 1); - const max_h_position: f32 = @floatFromInt(state.buffer.width - bb_width - 1); + const dheight = if (bb_height + 1 > state.buffer.height) 1 else state.buffer.height - bb_height - 1; + const dwidth = if (bb_width + 1 > state.buffer.width) 1 else state.buffer.width - bb_width - 1; + + const max_v_position: f32 = @floatFromInt(dheight); + const max_h_position: f32 = @floatFromInt(dwidth); bb_height = @min(bb_height, state.buffer.height - 2); bb_width = @min(bb_width, state.buffer.width - 2); const v_space: f32 = @floatFromInt(state.buffer.height - bb_height); - const v_position: usize = @intFromFloat(std.math.clamp(v_space * state.config.box_position_v, 1.0, max_v_position)); + const v_position: usize = @intFromFloat(if (max_v_position < 1.0) 1.0 else std.math.clamp(v_space * state.config.box_position_v, 1.0, max_v_position)); const h_space: f32 = @floatFromInt(state.buffer.width - bb_width); - const h_position: usize = @intFromFloat(std.math.clamp(h_space * state.config.box_position_h, 1.0, max_h_position)); + const h_position: usize = @intFromFloat(if (max_h_position < 1.0) 1.0 else std.math.clamp(h_space * state.config.box_position_h, 1.0, max_h_position)); if (state.config.bigclock != .none) { + const cwidth = if (clock_text_len > bb_width) 0 else bb_width - clock_text_len; + state.bigclock_label.positionXY(TerminalBuffer.START_POSITION - .addX(h_position + (bb_width - clock_text_len) / 2) + .addX(h_position + cwidth / 2) .addY(v_position)); } + const bwidth = if (state.box.width > bb_width) 0 else bb_width - state.box.width; + const bheight = if (state.box.height > bb_height) 0 else bb_height - state.box.height; + state.box.positionXY(TerminalBuffer.START_POSITION - .addX(h_position + (bb_width - state.box.width) / 2) - .addY(v_position + (bb_height - state.box.height))); + .addX(h_position + bwidth / 2) + .addY(v_position + bheight)); state.info_line.label.positionY(state.box .childrenPosition());