runtime: fix memory corruption and leak in recursive panic handling

Recursive panics leave dangling Panic structs in g->panic stack.
At best it leads to a Defer leak and incorrect output on a subsequent panic.
At worst it arbitrary corrupts heap.

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/72480043
This commit is contained in:
Dmitriy Vyukov 2014-03-07 20:50:30 +04:00
parent b08156cd87
commit f946a7ca09
3 changed files with 76 additions and 1 deletions

View file

@ -132,6 +132,18 @@ func TestThreadExhaustion(t *testing.T) {
}
}
func TestRecursivePanic(t *testing.T) {
output := executeTest(t, recursivePanicSource, nil)
want := `wrap: bad
panic: again
`
if !strings.HasPrefix(output, want) {
t.Fatalf("output does not start with %q:\n%s", want, output)
}
}
const crashSource = `
package main
@ -272,3 +284,29 @@ func main() {
}
}
`
const recursivePanicSource = `
package main
import (
"fmt"
)
func main() {
func() {
defer func() {
fmt.Println(recover())
}()
var x [8192]byte
func(x [8192]byte) {
defer func() {
if err := recover(); err != nil {
panic("wrap: " + err.(string))
}
}()
panic("bad")
}(x)
}()
panic("again")
}
`