Repository: jesseduffield/lazygit (language: go)

Issue:
SegFault with Commit panel -> Create a custom patch -> '?' -> '/' -> type "custom" to search -> 'Ctrl+p' -> Crash
**Describe the bug**
Bug when searching and then pressing `Ctrl-p` in commit menu.

**To Reproduce**
Steps to reproduce the behavior:

1. Go the the commits panel
2. Create a custom patch
3. Press '?' to open the keybindings menu
4. Press '/' to start search
5. Type "custom" to search to this text
6. Press 'Ctrl+p' (Custom patch action) to provoke a crash
9. See error

```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x68 pc=0xa08f53]

goroutine 1 [running]:
github.com/jesseduffield/lazygit/pkg/gui/context.(*MenuViewModel).GetNonModelItems(0xc0003c43f0)
	/home/runner/work/lazygit/lazygit/pkg/gui/context/menu_context.go:149 +0x153
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListRenderer).renderLines(0xc00005b190, 0xffffffffffffffff, 0xffffffffffffffff)
	/home/runner/work/lazygit/lazygit/pkg/gui/context/list_renderer.go:77 +0xa7
github.com/jesseduffield/lazygit/pkg/gui/context.(*ListContextTrait).HandleRender(0xc00005b180)
	/home/runner/work/lazygit/lazygit/pkg/gui/context/list_context_trait.go:108 +0x12a
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).postRefreshUpdate(0xc000468008, {0xf3c6c8, 0xc000010570})
	/home/runner/work/lazygit/lazygit/pkg/gui/view_helpers.go:140 +0xc7
github.com/jesseduffield/lazygit/pkg/gui.(*guiCommon).PostRefreshUpdate(0xc000468008?, {0xf3c6c8?, 0xc000010570?})
	/home/runner/work/lazygit/lazygit/pkg/gui/gui_common.go:33 +0x25
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).createMenu(0xc000468008, {{0xda6b5f, 0xd}, {0x0, 0x0}, {0xc0005a0600, 0x8, 0xc}, 0x0, {0x0, ...}})
	/home/runner/work/lazygit/lazygit/pkg/gui/menu_panel.go:60 +0x5cb
github.com/jesseduffield/lazygit/pkg/gui/popup.(*PopupHandler).Menu(0x1?, {{0xda6b5f, 0xd}, {0x0, 0x0}, {0xc0005a0600, 0x7, 0xc}, 0x0, {0x0, ...}})
	/home/runner/work/lazygit/lazygit/pkg/gui/popup/popup_handler.go:58 +0x3d
github.com/jesseduffield/lazygit/pkg/gui/controllers.(*CustomPatchOptionsMenuAction).Call(0xc00148e038)
	/home/runner/work/lazygit/lazygit/pkg/gui/controllers/custom_patch_options_menu_action.go:105 +0x1116
github.com/jesseduffield/lazygit/pkg/gui/controllers.(*GlobalController).createCustomPatchOptionsMenu(...)
	/home/runner/work/lazygit/lazygit/pkg/gui/controllers/global_controller.go:140
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).callKeybindingHandler(0xc000468008, 0xc00168d420?)
	/home/runner/work/lazygit/lazygit/pkg/gui/keybindings.go:452 +0x104
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).SetKeybinding.func1()
	/home/runner/work/lazygit/lazygit/pkg/gui/keybindings.go:405 +0x1b
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).SetKeybinding.(*Gui).wrappedHandler.func3(0xc00168d4a8?, 0x411d5b?)
	/home/runner/work/lazygit/lazygit/pkg/gui/keybindings.go:399 +0x13
github.com/jesseduffield/gocui.(*Gui).execKeybinding(0xc00090c000?, 0xc00168d58c?, 0x2?)
	/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:1473 +0x4f
github.com/jesseduffield/gocui.(*Gui).execKeybindings(0xc0004621c0, 0xc00001d400, 0xc00168d5a0)
	/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:1462 +0x49b
github.com/jesseduffield/gocui.(*Gui).onKey(0xc0003ba148?, 0xc00168d528?)
	/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:1298 +0xfe
github.com/jesseduffield/gocui.(*Gui).handleEvent(0xc0003ba120?, 0xc00168d588?)
	/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:828 +0x39
github.com/jesseduffield/gocui.(*Gui).processEvent(0xc0004621c0)
	/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:782 +0x22f
github.com/jesseduffield/gocui.(*Gui).MainLoop(0xc0004621c0)
	/home/runner/work/lazygit/lazygit/vendor/github.com/jesseduffield/gocui/gui.go:761 +0x9d
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).Run(0xc000468008, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
	/home/runner/work/lazygit/lazygit/pkg/gui/gui.go:701 +0x66a
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).RunAndHandleError.func1()
	/home/runner/work/lazygit/lazygit/pkg/gui/gui.go:707 +0x45
github.com/jesseduffield/lazygit/pkg/utils.SafeWithError(0x31?)
	/home/runner/work/lazygit/lazygit/pkg/utils/utils.go:99 +0x5c
github.com/jesseduffield/lazygit/pkg/gui.(*Gui).RunAndHandleError(0xc000468008, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
	/home/runner/work/lazygit/lazygit/pkg/gui/gui.go:706 +0xce
github.com/jesseduffield/lazygit/pkg/app.(*App).Run(...)
	/home/runner/work/lazygit/lazygit/pkg/app/app.go:272
github.com/jesseduffield/lazygit/pkg/app.Run({0xf34a70?, 0xc0003df400?}, 0xc00039e900, {{0x0, 0x0}, {0x0, 0x0}, {0x0, 0x0}})
	/home/runner/work/lazygit/lazygit/pkg/app/app.go:48 +0xd6
github.com/jesseduffield/lazygit/pkg/app.Start(0xc00020ff00, {0x0, 0x0})
	/home/runner/work/lazygit/lazygit/pkg/app/entry_point.go:161 +0xcf7
main.main()
	/home/runner/work/lazygit/lazygit/main.go:23 +0x98
```

**Expected behavior**
Should activate the "Custom patch option" as if no search were initiated

**Version info:**
- `lazygit --version`: `commit=71ad3fac63a3ef3326021837b49e9497d332818b, build date=2024-07-13T10:24:19Z, build source=binaryRelease, version=0.43.1, os=linux, arch=amd64, git version=2.34.1`
- `git --version`: git version 2.34.1

**Additional context**
Doesn't crash if no search is initiated.

**Note:** please try updating to the latest version or [manually building](https://github.com/jesseduffield/lazygit/#manual) the latest `master` to see if the issue still occurs.

<!--
If you want to try and debug this issue yourself, you can run `lazygit --debug` in one terminal panel and `lazygit --logs` in another to view the logs.
-->



Discussion / hints:

Fix patch:
diff --git a/pkg/gui/controllers/helpers/search_helper.go b/pkg/gui/controllers/helpers/search_helper.go
index 0480cd095ff..554c87af1de 100644
--- a/pkg/gui/controllers/helpers/search_helper.go
+++ b/pkg/gui/controllers/helpers/search_helper.go
@@ -45,7 +45,7 @@ func (self *SearchHelper) OpenFilterPrompt(context types.IFilterableContext) err
 		return err
 	}
 
-	return nil
+	return self.c.ResetKeybindings()
 }
 
 func (self *SearchHelper) OpenSearchPrompt(context types.ISearchableContext) error {
@@ -64,7 +64,7 @@ func (self *SearchHelper) OpenSearchPrompt(context types.ISearchableContext) err
 		return err
 	}
 
-	return nil
+	return self.c.ResetKeybindings()
 }
 
 func (self *SearchHelper) DisplayFilterStatus(context types.IFilterableContext) {
@@ -112,16 +112,21 @@ func (self *SearchHelper) Confirm() error {
 		return self.CancelPrompt()
 	}
 
+	var err error
 	switch state.SearchType() {
 	case types.SearchTypeFilter:
-		return self.ConfirmFilter()
+		err = self.ConfirmFilter()
 	case types.SearchTypeSearch:
-		return self.ConfirmSearch()
+		err = self.ConfirmSearch()
 	case types.SearchTypeNone:
-		return self.c.Context().Pop()
+		err = self.c.Context().Pop()
 	}
 
-	return nil
+	if err != nil {
+		return err
+	}
+
+	return self.c.ResetKeybindings()
 }
 
 func (self *SearchHelper) ConfirmFilter() error {
@@ -183,7 +188,11 @@ func modelSearchResults(context types.ISearchableContext) []gocui.SearchPosition
 func (self *SearchHelper) CancelPrompt() error {
 	self.Cancel()
 
-	return self.c.Context().Pop()
+	if err := self.c.Context().Pop(); err != nil {
+		return err
+	}
+
+	return self.c.ResetKeybindings()
 }
 
 func (self *SearchHelper) ScrollHistory(scrollIncrement int) {
diff --git a/pkg/gui/gui_common.go b/pkg/gui/gui_common.go
index 1c56301d977..b1540e6eb4c 100644
--- a/pkg/gui/gui_common.go
+++ b/pkg/gui/gui_common.go
@@ -140,6 +140,10 @@ func (self *guiCommon) CallKeybindingHandler(binding *types.Binding) error {
 	return self.gui.callKeybindingHandler(binding)
 }
 
+func (self *guiCommon) ResetKeybindings() error {
+	return self.gui.resetKeybindings()
+}
+
 func (self *guiCommon) IsAnyModeActive() bool {
 	return self.gui.helpers.Mode.IsAnyModeActive()
 }
diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go
index a3da576596c..bdfed1df7ab 100644
--- a/pkg/gui/keybindings.go
+++ b/pkg/gui/keybindings.go
@@ -5,6 +5,7 @@ import (
 	"log"
 
 	"github.com/jesseduffield/gocui"
+	"github.com/jesseduffield/lazygit/pkg/gui/context"
 	"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
 	"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
 	"github.com/jesseduffield/lazygit/pkg/gui/types"
@@ -345,6 +346,18 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
 }
 
 func (self *Gui) GetInitialKeybindingsWithCustomCommands() ([]*types.Binding, []*gocui.ViewMouseBinding) {
+	// if the search or filter prompt is open, we only want the keybindings for
+	// that context. It shouldn't be possible, for example, to open a menu while
+	// the prompt is showing; you first need to confirm or cancel the search/filter.
+	if currentContext := self.State.ContextMgr.Current(); currentContext.GetKey() == context.SEARCH_CONTEXT_KEY {
+		bindings := currentContext.GetKeybindings(self.c.KeybindingsOpts())
+		viewName := currentContext.GetViewName()
+		for _, binding := range bindings {
+			binding.ViewName = viewName
+		}
+		return bindings, nil
+	}
+
 	bindings, mouseBindings := self.GetInitialKeybindings()
 	customBindings, err := self.CustomCommandsClient.GetCustomCommandKeybindings()
 	if err != nil {
diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go
index 9b7ccf34ff9..9ae7f9ea05b 100644
--- a/pkg/gui/types/common.go
+++ b/pkg/gui/types/common.go
@@ -98,6 +98,8 @@ type IGuiCommon interface {
 	KeybindingsOpts() KeybindingsOpts
 	CallKeybindingHandler(binding *Binding) error
 
+	ResetKeybindings() error
+
 	// hopefully we can remove this once we've moved all our keybinding stuff out of the gui god struct.
 	GetInitialKeybindingsWithCustomCommands() ([]*Binding, []*gocui.ViewMouseBinding)
 


Test patch: