1
0
mirror of https://github.com/golang/go synced 2024-11-17 21:34:49 -07:00
go/test/fixedbugs/issue19658.go

101 lines
2.3 KiB
Go
Raw Normal View History

// run
runtime: replace panic(nil) with panic(new(runtime.PanicNilError)) Long ago we decided that panic(nil) was too unlikely to bother making a special case for purposes of recover. Unfortunately, it has turned out not to be a special case. There are many examples of code in the Go ecosystem where an author has written panic(nil) because they want to panic and don't care about the panic value. Using panic(nil) in this case has the unfortunate behavior of making recover behave as though the goroutine isn't panicking. As a result, code like: func f() { defer func() { if err := recover(); err != nil { log.Fatalf("panicked! %v", err) } }() call1() call2() } looks like it guarantees that call2 has been run any time f returns, but that turns out not to be strictly true. If call1 does panic(nil), then f returns "successfully", having recovered the panic, but without calling call2. Instead you have to write something like: func f() { done := false defer func() { if err := recover(); !done { log.Fatalf("panicked! %v", err) } }() call1() call2() done = true } which defeats nearly the whole point of recover. No one does this, with the result that almost all uses of recover are subtly broken. One specific broken use along these lines is in net/http, which recovers from panics in handlers and sends back an HTTP error. Users discovered in the early days of Go that panic(nil) was a convenient way to jump out of a handler up to the serving loop without sending back an HTTP error. This was a bug, not a feature. Go 1.8 added panic(http.ErrAbortHandler) as a better way to access the feature. Any lingering code that uses panic(nil) to abort an HTTP handler without a failure message should be changed to use http.ErrAbortHandler. Programs that need the old, unintended behavior from net/http or other packages can set GODEBUG=panicnil=1 to stop the run-time error. Uses of recover that want to detect panic(nil) in new programs can check for recover returning a value of type *runtime.PanicNilError. Because the new GODEBUG is used inside the runtime, we can't import internal/godebug, so there is some new machinery to cross-connect those in this CL, to allow a mutable GODEBUG setting. That won't be necessary if we add any other mutable GODEBUG settings in the future. The CL also corrects the handling of defaulted GODEBUG values in the runtime, for #56986. Fixes #25448. Change-Id: I2b39c7e83e4f7aa308777dabf2edae54773e03f5 Reviewed-on: https://go-review.googlesource.com/c/go/+/461956 Reviewed-by: Robert Griesemer <gri@google.com> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Russ Cox <rsc@golang.org>
2023-01-12 07:30:38 -07:00
//go:build !nacl && !js && !gccgo
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// ensure that panic(x) where x is a numeric type displays a readable number
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
)
const fn = `
package main
import "errors"
type S struct {
}
func (s S) String() string {
return "s-stringer"
}
func main() {
_ = errors.New
panic(%s(%s))
}
`
func main() {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tempDir)
tmpFile := filepath.Join(tempDir, "tmp.go")
for _, tc := range []struct {
Type string
Input string
Expect string
runtime: replace panic(nil) with panic(new(runtime.PanicNilError)) Long ago we decided that panic(nil) was too unlikely to bother making a special case for purposes of recover. Unfortunately, it has turned out not to be a special case. There are many examples of code in the Go ecosystem where an author has written panic(nil) because they want to panic and don't care about the panic value. Using panic(nil) in this case has the unfortunate behavior of making recover behave as though the goroutine isn't panicking. As a result, code like: func f() { defer func() { if err := recover(); err != nil { log.Fatalf("panicked! %v", err) } }() call1() call2() } looks like it guarantees that call2 has been run any time f returns, but that turns out not to be strictly true. If call1 does panic(nil), then f returns "successfully", having recovered the panic, but without calling call2. Instead you have to write something like: func f() { done := false defer func() { if err := recover(); !done { log.Fatalf("panicked! %v", err) } }() call1() call2() done = true } which defeats nearly the whole point of recover. No one does this, with the result that almost all uses of recover are subtly broken. One specific broken use along these lines is in net/http, which recovers from panics in handlers and sends back an HTTP error. Users discovered in the early days of Go that panic(nil) was a convenient way to jump out of a handler up to the serving loop without sending back an HTTP error. This was a bug, not a feature. Go 1.8 added panic(http.ErrAbortHandler) as a better way to access the feature. Any lingering code that uses panic(nil) to abort an HTTP handler without a failure message should be changed to use http.ErrAbortHandler. Programs that need the old, unintended behavior from net/http or other packages can set GODEBUG=panicnil=1 to stop the run-time error. Uses of recover that want to detect panic(nil) in new programs can check for recover returning a value of type *runtime.PanicNilError. Because the new GODEBUG is used inside the runtime, we can't import internal/godebug, so there is some new machinery to cross-connect those in this CL, to allow a mutable GODEBUG setting. That won't be necessary if we add any other mutable GODEBUG settings in the future. The CL also corrects the handling of defaulted GODEBUG values in the runtime, for #56986. Fixes #25448. Change-Id: I2b39c7e83e4f7aa308777dabf2edae54773e03f5 Reviewed-on: https://go-review.googlesource.com/c/go/+/461956 Reviewed-by: Robert Griesemer <gri@google.com> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Russ Cox <rsc@golang.org>
2023-01-12 07:30:38 -07:00
}{
{"", "nil", "panic: panic called with nil argument"},
{"errors.New", `"test"`, "panic: test"},
{"S", "S{}", "panic: s-stringer"},
{"byte", "8", "panic: 8"},
{"rune", "8", "panic: 8"},
{"int", "8", "panic: 8"},
{"int8", "8", "panic: 8"},
{"int16", "8", "panic: 8"},
{"int32", "8", "panic: 8"},
{"int64", "8", "panic: 8"},
{"uint", "8", "panic: 8"},
{"uint8", "8", "panic: 8"},
{"uint16", "8", "panic: 8"},
{"uint32", "8", "panic: 8"},
{"uint64", "8", "panic: 8"},
{"uintptr", "8", "panic: 8"},
{"bool", "true", "panic: true"},
{"complex64", "8 + 16i", "panic: (+8.000000e+000+1.600000e+001i)"},
{"complex128", "8+16i", "panic: (+8.000000e+000+1.600000e+001i)"},
{"string", `"test"`, "panic: test"}} {
b := bytes.Buffer{}
fmt.Fprintf(&b, fn, tc.Type, tc.Input)
err = ioutil.WriteFile(tmpFile, b.Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
cmd := exec.Command("go", "run", tmpFile)
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = &buf
cmd.Env = os.Environ()
cmd.Run() // ignore err as we expect a panic
out := buf.Bytes()
panicIdx := bytes.Index(out, []byte("panic: "))
if panicIdx == -1 {
log.Fatalf("expected a panic in output for %s, got: %s", tc.Type, out)
}
eolIdx := bytes.IndexByte(out[panicIdx:], '\n') + panicIdx
if panicIdx == -1 {
log.Fatalf("expected a newline in output for %s after the panic, got: %s", tc.Type, out)
}
out = out[0:eolIdx]
if string(out) != tc.Expect {
log.Fatalf("expected '%s' for panic(%s(%s)), got %s", tc.Expect, tc.Type, tc.Input, out)
}
}
}