positionWidgets: Less integer overflows possible

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion 2026-07-10 00:15:07 +02:00
parent d00780d6ce
commit 84950136c9
No known key found for this signature in database
3 changed files with 20 additions and 9 deletions

View File

@ -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| { if (inactivity_event_fn) |inactivity_fn| {

View File

@ -129,8 +129,10 @@ fn draw(self: *Lua) void {
cell.put(x, y) catch {}; cell.put(x, y) catch {};
if (self.lua_str) |str| if (self.lua_str) |str|
for (str, 0..) |c, i| { for (str, 0..) |c, i| {
const dwidth = @divFloor(self.width, 2);
const dlen = @divFloor(str.len, 2);
Cell.init(c, 0x00FFFFFF, 0).put( 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, self.margin + 5,
) catch {}; ) catch {};
}; };

View File

@ -2244,26 +2244,34 @@ fn positionWidgets(ptr: *anyopaque) !void {
bb_width = @max(bb_width, clock_text_len); bb_width = @max(bb_width, clock_text_len);
} }
const max_v_position: f32 = @floatFromInt(state.buffer.height - bb_height - 1); const dheight = if (bb_height + 1 > state.buffer.height) 1 else state.buffer.height - bb_height - 1;
const max_h_position: f32 = @floatFromInt(state.buffer.width - bb_width - 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_height = @min(bb_height, state.buffer.height - 2);
bb_width = @min(bb_width, state.buffer.width - 2); bb_width = @min(bb_width, state.buffer.width - 2);
const v_space: f32 = @floatFromInt(state.buffer.height - bb_height); 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_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) { 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 state.bigclock_label.positionXY(TerminalBuffer.START_POSITION
.addX(h_position + (bb_width - clock_text_len) / 2) .addX(h_position + cwidth / 2)
.addY(v_position)); .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 state.box.positionXY(TerminalBuffer.START_POSITION
.addX(h_position + (bb_width - state.box.width) / 2) .addX(h_position + bwidth / 2)
.addY(v_position + (bb_height - state.box.height))); .addY(v_position + bheight));
state.info_line.label.positionY(state.box state.info_line.label.positionY(state.box
.childrenPosition()); .childrenPosition());