From d62154db837fef880714f710bafbe0af94034b40 Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Tue, 7 Jan 2025 16:01:46 +0000 Subject: weak: don't panic when calling Value on a zero Pointer Currently weak.Pointer.Value will panic if the weak.Pointer is uninitialized (zero value) which goes against it's documentation. Fix this and add a test. While we're here, also add a test to ensure weak.Make[T](nil) is equivalent to the zero value of weak.Pointer[T]. Fixes #71153. Change-Id: I4d9196026360bc42a5bfcb33ce449131ec251dba Reviewed-on: https://go-review.googlesource.com/c/go/+/641095 Reviewed-by: David Finkel Auto-Submit: Michael Knyszek LUCI-TryBot-Result: Go LUCI Reviewed-by: Carlos Amedee --- src/weak/pointer.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/weak/pointer.go') diff --git a/src/weak/pointer.go b/src/weak/pointer.go index 50af0c2fdc..39c512e76d 100644 --- a/src/weak/pointer.go +++ b/src/weak/pointer.go @@ -78,6 +78,9 @@ func Make[T any](ptr *T) Pointer[T] { // If a weak pointer points to an object with a finalizer, then Value will // return nil as soon as the object's finalizer is queued for execution. func (p Pointer[T]) Value() *T { + if p.u == nil { + return nil + } return (*T)(runtime_makeStrongFromWeak(p.u)) } -- cgit v1.3