Fixes #61651
Change-Id: I27d581719e6bf38910f9d47dcf023bbff74ddf72
Reviewed-on: https://go-review.googlesource.com/c/go/+/514037
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
// returns poolLocal pool for the P and the P's id.
// Caller must call runtime_procUnpin() when done with the pool.
func (p *Pool) pin() (*poolLocal, int) {
+ // Check whether p is nil to get a panic.
+ // Otherwise the nil dereference happens while the m is pinned,
+ // causing a fatal error rather than a panic.
+ if p == nil {
+ panic("nil Pool")
+ }
+
pid := runtime_procPin()
// In pinSlow we store to local and then to localSize, here we load in opposite order.
// Since we've disabled preemption, GC cannot happen in between.
}
}
+func TestNilPool(t *testing.T) {
+ catch := func() {
+ if recover() == nil {
+ t.Error("expected panic")
+ }
+ }
+
+ var p *Pool
+ t.Run("Get", func(t *testing.T) {
+ defer catch()
+ if p.Get() != nil {
+ t.Error("expected empty")
+ }
+ t.Error("should have panicked already")
+ })
+ t.Run("Put", func(t *testing.T) {
+ defer catch()
+ p.Put("a")
+ t.Error("should have panicked already")
+ })
+}
+
func BenchmarkPool(b *testing.B) {
var p Pool
b.RunParallel(func(pb *testing.PB) {