From 28f7ab4651cbafbd9c6edc1bb6ce8763b638f52c Mon Sep 17 00:00:00 2001 From: Lionel DARNIS Date: Sat, 25 Jul 2026 22:20:01 +0200 Subject: [PATCH] fix(segmented_button): crash when enumerating context menu buttons Cherry-pick of upstream fix (pop-os/libcosmic#1354). The .enumerate().find_map() pattern counted ALL items (buttons + dividers) but the resulting index was used to access a children array containing only button entries. When a divider preceded the target button (e.g. trash in nav bar), the index was off-by-N, causing an out-of-bounds panic and crashing cosmic-files. Fix: filter_map first to keep only buttons, then enumerate. --- src/widget/segmented_button/widget.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/widget/segmented_button/widget.rs b/src/widget/segmented_button/widget.rs index 8c3a4ae2..61808fbe 100644 --- a/src/widget/segmented_button/widget.rs +++ b/src/widget/segmented_button/widget.rs @@ -981,11 +981,12 @@ where let Some((mut bounds, i)) = self .variant_bounds(state, layout.bounds()) - .enumerate() - .find_map(|(i, item)| match item { - ItemBounds::Button(e, bounds) if e == entity => Some((bounds, i)), + .filter_map(|item| match item { + ItemBounds::Button(entity, bounds) => Some((bounds, entity)), _ => None, }) + .enumerate() + .find_map(|(i, (bounds, e))| if e == entity { Some((bounds, i)) } else { None }) else { return; }; @@ -2581,11 +2582,12 @@ where let (mut bounds, i) = self .variant_bounds(state, layout.bounds()) - .enumerate() - .find_map(|(i, item)| match item { - ItemBounds::Button(e, bounds) if e == entity => Some((bounds, i)), + .filter_map(|item| match item { + ItemBounds::Button(entity, bounds) => Some((bounds, entity)), _ => None, - })?; + }) + .enumerate() + .find_map(|(i, (bounds, e))| if e == entity { Some((bounds, i)) } else { None })?; assert!( self.context_menu