From 13cef4e9081db10d587ffcbbeeffdf49c214e34a Mon Sep 17 00:00:00 2001 From: Richard Shaffer Date: Tue, 11 Jul 2017 15:42:04 -0700 Subject: [PATCH] Don't reset the length of re-used []uintptr slices. There seemed to be an invalid assumption that runtime.Callers(skip, pcbuf) would write and return up to cap(pcbuf) entries. This is incorrect--it will only write and return up to len(pcbuf). This change updates takeStacktrace() to avoid changing the len of re-used slices. --- stacktrace.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stacktrace.go b/stacktrace.go index 3d2a22c29..d2dc7cc43 100644 --- a/stacktrace.go +++ b/stacktrace.go @@ -46,12 +46,12 @@ func takeStacktrace() string { programCounters := _stacktracePool.Get().(*programCounters) defer _stacktracePool.Put(programCounters) + var numFrames int for { // Skip the call to runtime.Counters and takeStacktrace so that the // program counters start at the caller of takeStacktrace. - n := runtime.Callers(2, programCounters.pcs) - if n < cap(programCounters.pcs) { - programCounters.pcs = programCounters.pcs[:n] + numFrames = runtime.Callers(2, programCounters.pcs) + if numFrames < len(programCounters.pcs) { break } // Don't put the too-short counter slice back into the pool; this lets @@ -60,7 +60,7 @@ func takeStacktrace() string { } i := 0 - frames := runtime.CallersFrames(programCounters.pcs) + frames := runtime.CallersFrames(programCounters.pcs[:numFrames]) for frame, more := frames.Next(); more; frame, more = frames.Next() { if shouldIgnoreStacktraceFunction(frame.Function) { continue