From 5b7303817eb5fe9d63b05a129be33d36d50301c7 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Wed, 9 Oct 2024 14:03:37 -0700 Subject: [PATCH] syncs: allocate map with Map.WithLock (#13755) One primary purpose of WithLock is to mutate the underlying map. However, this can lead to a panic if it happens to be nil. Thus, always allocate a map before passing it to f. Updates tailscale/corp#11038 Signed-off-by: Joe Tsai --- syncs/syncs.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/syncs/syncs.go b/syncs/syncs.go index 4496581a6..acc0c88f2 100644 --- a/syncs/syncs.go +++ b/syncs/syncs.go @@ -304,6 +304,9 @@ func (m *Map[K, V]) All() iter.Seq2[K, V] { func (m *Map[K, V]) WithLock(f func(m2 map[K]V)) { m.mu.Lock() defer m.mu.Unlock() + if m.m == nil { + m.m = make(map[K]V) + } f(m.m) }