From 93c160ab66c0ecf6854dfc13ef3ebba26c3aefe8 Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Tue, 22 Mar 2022 00:50:02 +0100 Subject: [PATCH] threading: fix stack overflow from faulty headers Fix stack overflow from faulty headers that cause a circularity in the threading algorithm. Remove duplicated message-ids in the references headers. Check that the message-id itself is not part of the references. Fixes: https://todo.sr.ht/~rjarry/aerc/32 Signed-off-by: Koni Marti Tested-by: Robin Jarry Acked-by: Robin Jarry --- lib/threadbuilder.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/threadbuilder.go b/lib/threadbuilder.go index 8ffb7c1..9137fc1 100644 --- a/lib/threadbuilder.go +++ b/lib/threadbuilder.go @@ -200,7 +200,19 @@ func (t *threadable) MessageThreadReferences() []string { } refs = []string{inreplyto} } - return refs + return cleanRefs(t.MessageThreadID(), refs) +} + +func cleanRefs(m string, refs []string) []string { + considered := make(map[string]interface{}) + cleanRefs := make([]string, 0, len(refs)) + for _, r := range refs { + if _, seen := considered[r]; r != m && !seen { + considered[r] = nil + cleanRefs = append(cleanRefs, r) + } + } + return cleanRefs } func (t *threadable) Subject() string {