Skip to content

Commit 8122af0

Browse files
committed
Fix TypedValue.CompareAndSwap
tailscale/tailscale@84aa7ff
1 parent 2ce4c30 commit 8122af0

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

common/atomic/typed.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ func (t *TypedValue[T]) Swap(new T) T {
4242
}
4343

4444
func (t *TypedValue[T]) CompareAndSwap(old, new T) bool {
45-
return t.value.CompareAndSwap(typedValue[T]{old}, typedValue[T]{new})
45+
return t.value.CompareAndSwap(typedValue[T]{old}, typedValue[T]{new}) ||
46+
// In the edge-case where [atomic.Value.Store] is uninitialized
47+
// and trying to compare with the zero value of T,
48+
// then compare-and-swap with the nil any value.
49+
(any(old) == any(common.DefaultValue[T]()) && t.value.CompareAndSwap(any(nil), typedValue[T]{new}))
4650
}

0 commit comments

Comments
 (0)