From 8b859be9c3fd1068b659afa1db76dadb210c63de Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 12 Apr 2021 10:02:37 -0700 Subject: [PATCH] internal/poll: ensure that newPoolPipe doesn't return a nil pointer 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 Run-TryBot: Ian Lance Taylor Reviewed-by: Cuong Manh Le Reviewed-by: Andy Pan TryBot-Result: Go Bot --- src/internal/poll/splice_linux.go | 5 +++-- src/internal/poll/splice_linux_test.go | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/internal/poll/splice_linux.go b/src/internal/poll/splice_linux.go index 49350b1ddcd..8062d98fae9 100644 --- a/src/internal/poll/splice_linux.go +++ b/src/internal/poll/splice_linux.go @@ -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 } diff --git a/src/internal/poll/splice_linux_test.go b/src/internal/poll/splice_linux_test.go index 77ae912d543..3629ef1b20d 100644 --- a/src/internal/poll/splice_linux_test.go +++ b/src/internal/poll/splice_linux_test.go @@ -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) } })