]> Cypherpunks repositories - gostls13.git/commitdiff
internal/poll: ensure that newPoolPipe doesn't return a nil pointer
authorIan Lance Taylor <iant@golang.org>
Mon, 12 Apr 2021 17:02:37 +0000 (10:02 -0700)
committerIan Lance Taylor <iant@golang.org>
Mon, 12 Apr 2021 17:32:47 +0000 (17:32 +0000)
The function could occasionally return a nil pointer as a non-nil
interface, confusing the calling code.

Fixes #45520

Change-Id: Ifd35613728efa2cee9903177e85d369155074804
Reviewed-on: https://go-review.googlesource.com/c/go/+/309429
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Andy Pan <panjf2000@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/internal/poll/splice_linux.go
src/internal/poll/splice_linux_test.go

index 49350b1ddcd7481e1d37030240415cb26b05529f..8062d98fae945ccf214dc095ed8165e83dd556ad 100644 (file)
@@ -169,9 +169,10 @@ func newPoolPipe() interface{} {
        // Discard the error which occurred during the creation of pipe buffer,
        // redirecting the data transmission to the conventional way utilizing read() + write() as a fallback.
        p := newPipe()
-       if p != nil {
-               runtime.SetFinalizer(p, destroyPipe)
+       if p == nil {
+               return nil
        }
+       runtime.SetFinalizer(p, destroyPipe)
        return p
 }
 
index 77ae912d54319d79c1644885ba4ce37d89f308e2..3629ef1b20d641783effcc6278ad52c65cfab7c8 100644 (file)
@@ -75,13 +75,19 @@ func TestSplicePipePool(t *testing.T) {
 func BenchmarkSplicePipe(b *testing.B) {
        b.Run("SplicePipeWithPool", func(b *testing.B) {
                for i := 0; i < b.N; i++ {
-                       p, _, _ := poll.GetPipe()
+                       p, _, err := poll.GetPipe()
+                       if err != nil {
+                               continue
+                       }
                        poll.PutPipe(p)
                }
        })
        b.Run("SplicePipeWithoutPool", func(b *testing.B) {
                for i := 0; i < b.N; i++ {
                        p := poll.NewPipe()
+                       if p == nil {
+                               b.Skip("newPipe returned nil")
+                       }
                        poll.DestroyPipe(p)
                }
        })
@@ -90,7 +96,10 @@ func BenchmarkSplicePipe(b *testing.B) {
 func BenchmarkSplicePipePoolParallel(b *testing.B) {
        b.RunParallel(func(pb *testing.PB) {
                for pb.Next() {
-                       p, _, _ := poll.GetPipe()
+                       p, _, err := poll.GetPipe()
+                       if err != nil {
+                               continue
+                       }
                        poll.PutPipe(p)
                }
        })
@@ -100,6 +109,9 @@ func BenchmarkSplicePipeNativeParallel(b *testing.B) {
        b.RunParallel(func(pb *testing.PB) {
                for pb.Next() {
                        p := poll.NewPipe()
+                       if p == nil {
+                               b.Skip("newPipe returned nil")
+                       }
                        poll.DestroyPipe(p)
                }
        })