aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap/seqmap.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-06-20 21:49:05 +0200
committerRobin Jarry <robin@jarry.cc>2022-06-24 21:08:12 +0200
commit420f236d31da76df9982331c596b776ab3d0dd76 (patch)
treedf1c0ee7612400aefd08f0ce260bbb421d50b491 /worker/imap/seqmap.go
parent389c0f82a7f5dc250500fcff6570308cc3bcbd88 (diff)
downloadaerc-420f236d31da76df9982331c596b776ab3d0dd76.tar.gz
imap: fix data race on seqMap array
There are concurrent threads that are accessing and modifying IMAPWorker.seqMap (the mapping of sequence numbers to message UIDs). This can lead to crashes when trying to add and remove a message ID. panic: runtime error: index out of range [391] with length 390 goroutine 1834 [running]: git.sr.ht/~rjarry/aerc/logging.PanicHandler() logging/panic-logger.go:47 +0x6de panic({0xa41760, 0xc0019b3290}) /usr/lib/golang/src/runtime/panic.go:838 +0x207 git.sr.ht/~rjarry/aerc/worker/imap.(*IMAPWorker).handleFetchMessages.func1() worker/imap/fetch.go:214 +0x185 created by git.sr.ht/~rjarry/aerc/worker/imap.(*IMAPWorker).handleFetchMessages worker/imap/fetch.go:209 +0x12b Use a map which makes more sense than a simple array for random access operations. Also, it allows better typing for the key values. Protect the map with a mutex. Add internal API to access the map. Add basic unit tests to ensure that concurrent access works. Fixes: https://todo.sr.ht/~rjarry/aerc/49 Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'worker/imap/seqmap.go')
-rw-r--r--worker/imap/seqmap.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/worker/imap/seqmap.go b/worker/imap/seqmap.go
new file mode 100644
index 00000000..2752cc87
--- /dev/null
+++ b/worker/imap/seqmap.go
@@ -0,0 +1,48 @@
+package imap
+
+import "sync"
+
+type SeqMap struct {
+ lock sync.Mutex
+ // map of IMAP sequence numbers to message UIDs
+ m map[uint32]uint32
+}
+
+func (s *SeqMap) Size() int {
+ s.lock.Lock()
+ size := len(s.m)
+ s.lock.Unlock()
+ return size
+}
+
+func (s *SeqMap) Get(seqnum uint32) (uint32, bool) {
+ s.lock.Lock()
+ uid, found := s.m[seqnum]
+ s.lock.Unlock()
+ return uid, found
+}
+
+func (s *SeqMap) Put(seqnum, uid uint32) {
+ s.lock.Lock()
+ if s.m == nil {
+ s.m = make(map[uint32]uint32)
+ }
+ s.m[seqnum] = uid
+ s.lock.Unlock()
+}
+
+func (s *SeqMap) Pop(seqnum uint32) (uint32, bool) {
+ s.lock.Lock()
+ uid, found := s.m[seqnum]
+ if found {
+ delete(s.m, seqnum)
+ }
+ s.lock.Unlock()
+ return uid, found
+}
+
+func (s *SeqMap) Clear() {
+ s.lock.Lock()
+ s.m = make(map[uint32]uint32)
+ s.lock.Unlock()
+}