Skip to content

Conversation

Jack251970
Copy link
Member

Check that index >= 0 && index < _searchSources.Count before accessing the collection and show message box if necessary

Fix #4036

Copilot AI and others added 5 commits October 10, 2025 04:06
Add bounds checking and fallback logic to handle cases where the search source
reference is not found in the collection. This prevents the crash when editing
a search source that may have been modified or removed from the collection.

Co-authored-by: Jack251970 <[email protected]>
…ettings

Add defensive programming checks to CustomHotkeyEdit and CustomShortcutEdit methods
to prevent potential IndexOutOfRangeException in rare edge cases where collection
might be modified concurrently.

Co-authored-by: Jack251970 <[email protected]>
Added a new localized string `flowlauncher_plugin_websearch_edit_failed` in `en.xaml` for handling errors when updating a search source. Updated `SearchSourceSetting.xaml.cs` to use this localized string and removed fallback logic for default error messages.
Removed fallback mechanism for locating a search source by
`ActionKeyword` when not found by reference. The update now
proceeds only if the search source is found by reference,
streamlining the logic and reducing complexity.
@prlabeler prlabeler bot added the bug Something isn't working label Oct 11, 2025
@github-actions github-actions bot added this to the 2.1.0 milestone Oct 11, 2025
@Jack251970 Jack251970 requested a review from Copilot October 11, 2025 02:11
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an ArgumentOutOfRangeException in the WebSearch plugin and similar issues in settings by adding bounds checking before accessing collections using IndexOf results. The fix prevents crashes when trying to update items that may have been removed from collections between retrieval and modification.

  • Added bounds checking (index >= 0 && index < collection.Count) before accessing collections
  • Added proper error handling with localized warning messages for failed operations
  • Applied the same defensive pattern across multiple similar operations

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
SearchSourceSetting.xaml.cs Added bounds checking and error handling for search source editing
en.xaml Added localized error message for failed search source updates
SettingsPaneHotkeyViewModel.cs Added bounds checking for hotkey and shortcut editing operations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link

gitstream-cm bot commented Oct 11, 2025

🥷 Code experts: onesounds, Yusyuriv

onesounds, Jack251970 have most 👩‍💻 activity in the files.
onesounds, Jack251970 have most 🧠 knowledge in the files.

See details

Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs

Activity based on git-commit:

onesounds Jack251970
OCT
SEP 10 additions & 14 deletions
AUG
JUL 32 additions & 6 deletions
JUN
MAY

Knowledge based on git-blame:
Jack251970: 26%

Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml

Activity based on git-commit:

onesounds Jack251970
OCT
SEP
AUG
JUL
JUN
MAY

Knowledge based on git-blame:
onesounds: 33%
Jack251970: 2%

Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs

Activity based on git-commit:

onesounds Jack251970
OCT
SEP
AUG
JUL
JUN
MAY

Knowledge based on git-blame:
onesounds: 15%
Jack251970: 8%

✨ Comment /gs review for LinearB AI review. Learn how to automate it here.

Copy link

gitstream-cm bot commented Oct 11, 2025

Be a legend 🏆 by adding a before and after screenshot of the changes you made, especially if they are around UI/UX.

Copy link
Contributor

coderabbitai bot commented Oct 11, 2025

📝 Walkthrough

Walkthrough

Adds bounds checks before updating collections in hotkey and web search edit flows. If an index is invalid, skip the update; for web search, show a new localized warning. Localization adds a new English string for edit failure. No public APIs changed.

Changes

Cohort / File(s) Summary
Hotkey settings bounds checks
Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs
Guard updates in CustomHotkeyEdit and CustomShortcutEdit with index range checks before writing to Settings.CustomPluginHotkeys and Settings.CustomShortcuts. Preserve subsequent removal/assignment behavior where applicable.
WebSearch edit flow + localization
Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs, Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml
EditSearchSource now validates the located index before updating _searchSources; on invalid index, shows a new localized warning (flowlauncher_plugin_websearch_edit_failed) and closes. Added the corresponding English localization string.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant UI as SearchSourceSettingWindow
  participant List as _searchSources

  User->>UI: Confirm edit
  UI->>UI: Find index of selected source
  alt Index within bounds
    UI->>List: Update item at index
    UI-->>User: Close dialog
  else Index invalid
    UI-->>User: Show "edit failed" warning
    UI-->>User: Close dialog
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • jjw24

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Out of Scope Changes Check ⚠️ Warning The pull request includes unrelated modifications in SettingsPaneHotkeyViewModel.cs that add index checks for custom hotkeys and shortcuts, which do not pertain to the referenced WebSearch plugin bug fix and thus fall outside the scope of issue #4036. These hotkey-related changes should be moved to a separate pull request to keep the WebSearch plugin fix focused on the linked issue, or clearly documented as intentional additional enhancements.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title accurately and concisely captures the main change by indicating the fix for the out-of-range exception in the WebSearch plugin, reflecting the core purpose of the changeset.
Linked Issues Check ✅ Passed The pull request implements the essential fix from issue #4036 by validating that the index is within range before modifying the _searchSources collection and displaying an error message on failure, fully addressing the reported ArgumentOutOfRangeException scenario.
Description Check ✅ Passed The description clearly relates to the implemented changes by summarizing the added index validation and error messaging logic and references the fixed issue, demonstrating its relevance to the patch.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch copilot/fix-websearch-plugin-error

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs (2)

92-97: Approve bounds check; consider adding user feedback.

The bounds check correctly prevents ArgumentOutOfRangeException when the item is removed from the collection between the FirstOrDefault check and the IndexOf call. This scenario can occur if the user opens the edit dialog, then switches to another window to delete the item, and switches back to confirm the edit.

However, the silent failure may confuse users—their edit won't be saved, and they won't know why. Consider showing a message similar to the WebSearch plugin's approach (lines 118-122 in SearchSourceSetting.xaml.cs).

If you'd like to add user feedback, apply this diff:

 var index = Settings.CustomPluginHotkeys.IndexOf(settingItem);
 if (index >= 0 && index < Settings.CustomPluginHotkeys.Count)
 {
     Settings.CustomPluginHotkeys[index] = new CustomPluginHotkey(window.Hotkey, window.ActionKeyword);
     HotKeyMapper.RemoveHotkey(settingItem.Hotkey); // remove origin hotkey
     HotKeyMapper.SetCustomQueryHotkey(Settings.CustomPluginHotkeys[index]); // set new hotkey
 }
+else
+{
+    App.API.ShowMsgBox(Localize.invalidPluginHotkey());
+}

156-159: Approve bounds check; consider adding user feedback.

The bounds check correctly prevents ArgumentOutOfRangeException. However, like CustomHotkeyEdit, the silent failure may confuse users when their edit isn't saved.

If you'd like to add user feedback for consistency, apply this diff:

 var index = Settings.CustomShortcuts.IndexOf(settingItem);
 if (index >= 0 && index < Settings.CustomShortcuts.Count)
 {
     Settings.CustomShortcuts[index] = new CustomShortcutModel(window.Key, window.Value);
 }
+else
+{
+    App.API.ShowMsgBox(Localize.invalidShortcut());
+}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5ae159d and fc31cf3.

📒 Files selected for processing (3)
  • Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs (2 hunks)
  • Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml (1 hunks)
  • Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: Jack251970
PR: Flow-Launcher/Flow.Launcher#3593
File: Plugins/Flow.Launcher.Plugin.WebSearch/SettingsControl.xaml.cs:143-165
Timestamp: 2025-09-13T06:04:26.977Z
Learning: In Plugins/Flow.Launcher.Plugin.WebSearch/SettingsControl.xaml, the ListView_SizeChanged handler in the C# file assumes 5 GridViewColumns but the XAML may have more columns. The user Jack251970 indicated this mismatch doesn't need to be fixed when pointed out during PR #3593 review.
🧬 Code graph analysis (1)
Flow.Launcher/SettingPages/ViewModels/SettingsPaneHotkeyViewModel.cs (3)
Flow.Launcher.Infrastructure/UserSettings/Settings.cs (1)
  • Settings (15-615)
Flow.Launcher/Helper/HotKeyMapper.cs (3)
  • HotKeyMapper (13-168)
  • RemoveHotkey (96-119)
  • SetCustomQueryHotkey (138-148)
Flow.Launcher.Infrastructure/UserSettings/CustomShortcutModel.cs (1)
  • CustomShortcutModel (69-79)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: gitStream.cm
  • GitHub Check: gitStream.cm
  • GitHub Check: build
  • GitHub Check: gitStream.cm
  • GitHub Check: gitStream.cm
🔇 Additional comments (2)
Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs (1)

111-123: LGTM! Excellent user feedback on edge case.

The bounds check correctly prevents ArgumentOutOfRangeException, and the user-facing warning message provides clear feedback when the update fails. This implementation handles the TOCTOU scenario where the item could be removed from the collection while the edit dialog is open (e.g., user switches to another window to delete the item, then returns to confirm the edit).

Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml (1)

50-50: LGTM! Clear and helpful error message.

The localization string provides clear feedback to users when an edit operation fails due to an invalid index. The message appropriately explains the likely cause.

@jjw24 jjw24 enabled auto-merge (squash) October 11, 2025 07:33
@jjw24 jjw24 merged commit b297f3d into dev Oct 11, 2025
23 checks passed
@jjw24 jjw24 deleted the copilot/fix-websearch-plugin-error branch October 11, 2025 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: WebSearch Plugin Edit Search Source ArgumentOutOfRangeException

3 participants