Skip to content

Commit 7faf702

Browse files
runtime: avoid endless loop if printing the panic value panics
Change-Id: I56de359a5ccdc0a10925cd372fa86534353c6ca0 Reviewed-on: https://go-review.googlesource.com/30358 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent efaa360 commit 7faf702

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/runtime/crash_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,13 @@ func TestPanicDeadlockSyscall(t *testing.T) {
444444
testPanicDeadlock(t, "SyscallInPanic", "1\n2\npanic: 3\n\n")
445445
}
446446

447+
func TestPanicLoop(t *testing.T) {
448+
output := runTestProg(t, "testprog", "PanicLoop")
449+
if want := "panic while printing panic value"; !strings.Contains(output, want) {
450+
t.Errorf("output does not contain %q:\n%s", want, output)
451+
}
452+
}
453+
447454
func TestMemPprof(t *testing.T) {
448455
testenv.MustHaveGoRun(t)
449456

src/runtime/panic.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ func Goexit() {
376376
// Used when crashing with panicking.
377377
// This must match types handled by printany.
378378
func preprintpanics(p *_panic) {
379+
defer func() {
380+
if recover() != nil {
381+
throw("panic while printing panic value")
382+
}
383+
}()
379384
for p != nil {
380385
switch v := p.arg.(type) {
381386
case error:

src/runtime/testdata/testprog/deadlock.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func init() {
3232
register("PanicTraceback", PanicTraceback)
3333
register("GoschedInPanic", GoschedInPanic)
3434
register("SyscallInPanic", SyscallInPanic)
35+
register("PanicLoop", PanicLoop)
3536
}
3637

3738
func SimpleDeadlock() {
@@ -214,3 +215,13 @@ func pt2() {
214215
}()
215216
panic("hello")
216217
}
218+
219+
type panicError struct{}
220+
221+
func (*panicError) Error() string {
222+
panic("double error")
223+
}
224+
225+
func PanicLoop() {
226+
panic(&panicError{})
227+
}

0 commit comments

Comments
 (0)