From 84950136c96a7e22e2dbbd29da74e1ffd1d102bb Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Fri, 10 Jul 2026 00:15:07 +0200 Subject: [PATCH 1/2] 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()); From 7c2ae2738ca71c4df3b590c9ed4068911c23b527 Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Fri, 10 Jul 2026 13:02:58 +0200 Subject: [PATCH 2/2] positionSingleWidget: Resist integer overflows Signed-off-by: AnErrupTion --- src/main.zig | 75 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/src/main.zig b/src/main.zig index 382ace4..31a8519 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1950,8 +1950,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.shutdown_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.shutdown_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.shutdown_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "restart")) { @@ -1961,8 +1962,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.restart_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.restart_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.restart_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "britup")) { @@ -1972,8 +1974,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.brightness_up_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.brightness_up_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.brightness_up_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "britdown")) { @@ -1983,8 +1986,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.brightness_down_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.brightness_down_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.brightness_down_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "password")) { @@ -1994,8 +1998,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.toggle_password_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.toggle_password_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.toggle_password_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "clock") or std.mem.eql(u8, item, "time")) { @@ -2005,8 +2010,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.clock_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.clock_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.clock_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "tty")) { @@ -2015,8 +2021,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.tty_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.tty_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.tty_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "battery")) { @@ -2026,8 +2033,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.battery_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.battery_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.battery_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "version")) { @@ -2036,8 +2044,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.version_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.version_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.version_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "numlock")) { @@ -2046,8 +2055,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.numlock_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.numlock_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.numlock_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "capslock")) { @@ -2056,8 +2066,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu state.capslock_label.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - state.capslock_label.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + state.capslock_label.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } return true; } else if (std.mem.eql(u8, item, "labels")) { @@ -2068,8 +2079,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu info.lbl.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - info.lbl.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + info.lbl.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } positioned.labels[i] = true; } @@ -2094,8 +2106,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu local_x = state.buffer.width - state.edge_margin.x; if (is_top) local_y += 1 else local_y -= 1; } - bind.lbl.positionXY(Position.init(local_x - width, local_y)); - local_x -= width + 1; + const dwidth = if (width > local_x) 0 else local_x - width; + bind.lbl.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= local_x) local_x -= width + 1; } positioned.binds[i] = true; } @@ -2111,8 +2124,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu info.lbl.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - info.lbl.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + info.lbl.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } positioned.labels[i] = true; return true; @@ -2128,8 +2142,9 @@ fn positionSingleWidget(state: *UiState, item: []const u8, current_x: *usize, cu bind.lbl.positionXY(Position.init(current_x.*, current_y)); current_x.* += width + 1; } else { - bind.lbl.positionXY(Position.init(current_x.* - width, current_y)); - current_x.* -= width + 1; + const dwidth = if (width > current_x.*) 0 else current_x.* - width; + bind.lbl.positionXY(Position.init(dwidth, current_y)); + if (width + 1 <= current_x.*) current_x.* -= width + 1; } positioned.binds[i] = true; return true;