Skip to content

Commit

Permalink
Fix use of runtime.Callers when taking stack traces (#477)
Browse files Browse the repository at this point in the history
The current code incorrectly assumes that `runtime.Callers(skip, pcbuf)`
writes and returns up to `cap(pcbuf)` entries. This is incorrect: it only
writes and returns up to `len(pcbuf)`. This change updates `takeStacktrace` to
avoid changing the length of re-used slices.
  • Loading branch information
richard-tunein authored and akshayjshah committed Jul 22, 2017
1 parent b6ea2ee commit 770b97b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 770b97b

Please sign in to comment.