From 55de3bc26ccb3f5fa610c22821ad3d3c2ae0a6db Mon Sep 17 00:00:00 2001 From: matcxz Date: Wed, 30 Sep 2026 17:19:38 +0200 Subject: [PATCH] fix: prevent underflow when session list is empty (#1060) ## What are the changes about? Fixes an integer underflow in `min_session_index`, which was computed by subtracting 1 from the session list length without checking for zero. Caused a panic (Debug/ReleaseSafe) or wraparound (ReleaseFast) on installs with no available sessions. ## What existing issue(s) does this resolve? N/A ## Pre-requisites - [x] I have tested & confirmed the changes work locally - [x] I have read and fully adhere to the rules set in the contributing guidelines found in `CONTRIBUTING.md` Co-authored-by: Matheus C. <245040149+matheusc457@users.noreply.github.com> Reviewed-on: https://codeberg.org/fairyglade/ly/pulls/1060 Reviewed-by: AnErrupTion --- ly-ui/src/components/generic.zig | 2 ++ src/main.zig | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/ly-ui/src/components/generic.zig b/ly-ui/src/components/generic.zig index 2d0b3c4..76de5b4 100644 --- a/ly-ui/src/components/generic.zig +++ b/ly-ui/src/components/generic.zig @@ -140,6 +140,7 @@ pub fn CyclableLabel(comptime ItemType: type, comptime ChangeItemType: type) typ fn goLeft(ptr: *anyopaque) !bool { var self: *Self = @ptrCast(@alignCast(ptr)); + if (self.list.items.len == 0) return false; self.current = if (self.current == 0) self.list.items.len - 1 else self.current - 1; if (self.change_item_fn) |change_item_fn| { @@ -156,6 +157,7 @@ pub fn CyclableLabel(comptime ItemType: type, comptime ChangeItemType: type) typ fn goRight(ptr: *anyopaque) !bool { var self: *Self = @ptrCast(@alignCast(ptr)); + if (self.list.items.len == 0) return false; self.current = if (self.current == self.list.items.len - 1) 0 else self.current + 1; if (self.change_item_fn) |change_item_fn| { diff --git a/src/main.zig b/src/main.zig index 4d6bb65..489b46b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1148,7 +1148,7 @@ pub fn main(init: std.process.Init) !void { // Skip if autologin is active to prevent overriding autologin session var default_input = state.config.default_input; - const min_session_index = state.session.label.list.items.len - 1; + const min_session_index = if (state.session.label.list.items.len == 0) 0 else state.session.label.list.items.len - 1; if (state.config.save_file_dir != null and !state.is_autologin) { if (state.login_text) |box| {