]> Cypherpunks repositories - gostls13.git/commitdiff
sync: panic rather than throw on nil *Pool
authorIan Lance Taylor <iant@golang.org>
Sat, 29 Jul 2023 15:59:20 +0000 (08:59 -0700)
committerGopher Robot <gobot@golang.org>
Mon, 31 Jul 2023 22:18:47 +0000 (22:18 +0000)
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>

src/sync/pool.go
src/sync/pool_test.go

index cf01e2e1891a10d3feabf547c8721fb3d6ab3bec..ffab67bf19eb673c91068e312a51823cbc643404 100644 (file)
@@ -196,6 +196,13 @@ func (p *Pool) getSlow(pid int) any {
 // 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.
index 5e385974414cbfeed444e2675091e7e1d149cb75..1b6746dbfbaf9c2be854c1d53ddfa2bbc756cc9f 100644 (file)
@@ -247,6 +247,28 @@ func testPoolDequeue(t *testing.T, d PoolDequeue) {
        }
 }
 
+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) {