Skip to content

Commit 29b36a8

Browse files
committed
cmd/objdump: guard against out-of-range lines from directives.
//line bogo.go:9999999 will cause 'go tool objdump' to crash unless bogo.go has that many lines. Guard the array index and return innocuous values (nil, nil) from the file cache. Fixes #36683 Change-Id: I4a9f8444dc611654d270cc876e8848dfd2f84770 Reviewed-on: https://go-review.googlesource.com/c/go/+/223081 Run-TryBot: David Chase <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent e221a75 commit 29b36a8

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

src/cmd/internal/objfile/disasm.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ func (fc *FileCache) Line(filename string, line int) ([]byte, error) {
175175
fc.files.MoveToFront(e)
176176
}
177177

178+
// because //line directives can be out-of-range. (#36683)
179+
if line-1 >= len(cf.Lines) || line-1 < 0 {
180+
return nil, nil
181+
}
182+
178183
return cf.Lines[line-1], nil
179184
}
180185

src/cmd/objdump/objdump_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ func testDisasm(t *testing.T, printCode bool, flags ...string) {
106106
hello := filepath.Join(tmp, fmt.Sprintf("hello-%x.exe", hash))
107107
args := []string{"build", "-o", hello}
108108
args = append(args, flags...)
109-
args = append(args, "testdata/fmthello.go")
110-
out, err := exec.Command(testenv.GoToolPath(t), args...).CombinedOutput()
109+
args = append(args, "fmthello.go")
110+
cmd := exec.Command(testenv.GoToolPath(t), args...)
111+
cmd.Dir = "testdata" // "Bad line" bug #36683 is sensitive to being run in the source directory
112+
t.Logf("Running %v", cmd.Args)
113+
out, err := cmd.CombinedOutput()
111114
if err != nil {
112115
t.Fatalf("go build fmthello.go: %v\n%s", err, out)
113116
}
@@ -139,7 +142,11 @@ func testDisasm(t *testing.T, printCode bool, flags ...string) {
139142
args = append([]string{"-S"}, args...)
140143
}
141144

142-
out, err = exec.Command(exe, args...).CombinedOutput()
145+
cmd = exec.Command(exe, args...)
146+
cmd.Dir = "testdata" // "Bad line" bug #36683 is sensitive to being run in the source directory
147+
out, err = cmd.CombinedOutput()
148+
t.Logf("Running %v", cmd.Args)
149+
143150
if err != nil {
144151
t.Fatalf("objdump fmthello.exe: %v\n%s", err, out)
145152
}

src/cmd/objdump/testdata/fmthello.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import "fmt"
55
func main() {
66
Println("hello, world")
77
if flag {
8+
//line fmthello.go:999999
9+
Println("bad line")
810
for {
911
}
1012
}

test/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ func goGcflags() string {
463463
}
464464

465465
func goGcflagsIsEmpty() bool {
466-
return "" == os.Getenv("GO_GCFLAGS")
466+
return "" == os.Getenv("GO_GCFLAGS")
467467
}
468468

469469
// run runs a test.

0 commit comments

Comments
 (0)