aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/trace/pprof.go2
-rw-r--r--src/cmd/trace/pprof_test.go26
2 files changed, 27 insertions, 1 deletions
diff --git a/src/cmd/trace/pprof.go b/src/cmd/trace/pprof.go
index b472ffa759..f1ab959fa7 100644
--- a/src/cmd/trace/pprof.go
+++ b/src/cmd/trace/pprof.go
@@ -320,9 +320,9 @@ func (m *stackMap) profile() []traceviewer.ProfileRecord {
// pcsForStack extracts the first pprofMaxStack PCs from stack into pcs.
func pcsForStack(stack trace.Stack, pcs *[pprofMaxStack]uint64) {
for i, frame := range slices.Collect(stack.Frames()) {
- pcs[i] = frame.PC
if i >= len(pcs) {
break
}
+ pcs[i] = frame.PC
}
}
diff --git a/src/cmd/trace/pprof_test.go b/src/cmd/trace/pprof_test.go
index 6d18e7d5d1..ec5bbd6816 100644
--- a/src/cmd/trace/pprof_test.go
+++ b/src/cmd/trace/pprof_test.go
@@ -14,6 +14,7 @@ import (
"testing/synctest"
"time"
+ itrace "internal/trace"
"internal/trace/testtrace"
)
@@ -81,6 +82,31 @@ func TestSyscallProfile74850(t *testing.T) {
}
}
+// Regression test: pcsForStack must not panic when the stack has more than
+// pprofMaxStack (128) frames. Before the fix, pcs[i] was written before
+// checking i >= len(pcs), causing an index-out-of-range panic.
+func TestFillPCsOverflow(t *testing.T) {
+ var pcs [pprofMaxStack]uint64
+
+ // Build a stack with more frames than pprofMaxStack.
+ n := pprofMaxStack + 10
+ frames := make([]itrace.StackFrame, n)
+ for i := range frames {
+ frames[i].PC = uint64(0x1000 + i)
+ }
+ stack := itrace.MakeStack(frames)
+
+ // This must not panic.
+ pcsForStack(stack, &pcs)
+
+ // Verify the first pprofMaxStack entries were written correctly.
+ for i := 0; i < pprofMaxStack; i++ {
+ if pcs[i] != uint64(0x1000+i) {
+ t.Errorf("pcs[%d] = %#x, want %#x", i, pcs[i], 0x1000+i)
+ }
+ }
+}
+
func stat(t *testing.T) {
_, err := os.Stat(".")
if err != nil {