From 34014d3ceeebe8a9c131213fa56d1977fbc26b4a Mon Sep 17 00:00:00 2001
From: Robin Jarry <robin@jarry.cc>
Date: Mon, 10 Oct 2022 11:32:12 +0200
Subject: [PATCH] compose: avoid deadlock when adding new header

AddEditor acquires the lock and calls FocusEditor which also attempts to
acquire it. Since the lock is not re-entrant, it ends in deadlock.

Add an internal focusEditor fonction that does not acquire the lock.

Fixes: bf2bf8c242cb ("compose: prevent out of bounds access")
Reported-by: Moritz Poldrack <moritz@poldrack.dev>
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
---
 widgets/compose.go | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/widgets/compose.go b/widgets/compose.go
index 0c26d07..9b8b30c 100644
--- a/widgets/compose.go
+++ b/widgets/compose.go
@@ -958,6 +958,10 @@ func (c *Composer) NextField() {
 func (c *Composer) FocusEditor(editor string) {
 	c.Lock()
 	defer c.Unlock()
+	c.focusEditor(editor)
+}
+
+func (c *Composer) focusEditor(editor string) {
 	editor = strings.ToLower(editor)
 	c.focusable[c.focused].Focus(false)
 	for i, f := range c.focusable {
@@ -1007,7 +1011,7 @@ func (c *Composer) AddEditor(header string, value string, appendHeader bool) {
 		editor.storeValue()
 	}
 	if value == "" {
-		c.FocusEditor(c.editors[header].name)
+		c.focusEditor(c.editors[header].name)
 	}
 	c.updateGrid()
 }