mirror of
https://github.com/golang/go
synced 2024-11-23 09:10:08 -07:00
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 <drchase@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
parent
e221a75dea
commit
29b36a88ab
@ -175,6 +175,11 @@ func (fc *FileCache) Line(filename string, line int) ([]byte, error) {
|
||||
fc.files.MoveToFront(e)
|
||||
}
|
||||
|
||||
// because //line directives can be out-of-range. (#36683)
|
||||
if line-1 >= len(cf.Lines) || line-1 < 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return cf.Lines[line-1], nil
|
||||
}
|
||||
|
||||
|
@ -106,8 +106,11 @@ func testDisasm(t *testing.T, printCode bool, flags ...string) {
|
||||
hello := filepath.Join(tmp, fmt.Sprintf("hello-%x.exe", hash))
|
||||
args := []string{"build", "-o", hello}
|
||||
args = append(args, flags...)
|
||||
args = append(args, "testdata/fmthello.go")
|
||||
out, err := exec.Command(testenv.GoToolPath(t), args...).CombinedOutput()
|
||||
args = append(args, "fmthello.go")
|
||||
cmd := exec.Command(testenv.GoToolPath(t), args...)
|
||||
cmd.Dir = "testdata" // "Bad line" bug #36683 is sensitive to being run in the source directory
|
||||
t.Logf("Running %v", cmd.Args)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("go build fmthello.go: %v\n%s", err, out)
|
||||
}
|
||||
@ -139,7 +142,11 @@ func testDisasm(t *testing.T, printCode bool, flags ...string) {
|
||||
args = append([]string{"-S"}, args...)
|
||||
}
|
||||
|
||||
out, err = exec.Command(exe, args...).CombinedOutput()
|
||||
cmd = exec.Command(exe, args...)
|
||||
cmd.Dir = "testdata" // "Bad line" bug #36683 is sensitive to being run in the source directory
|
||||
out, err = cmd.CombinedOutput()
|
||||
t.Logf("Running %v", cmd.Args)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("objdump fmthello.exe: %v\n%s", err, out)
|
||||
}
|
||||
|
2
src/cmd/objdump/testdata/fmthello.go
vendored
2
src/cmd/objdump/testdata/fmthello.go
vendored
@ -5,6 +5,8 @@ import "fmt"
|
||||
func main() {
|
||||
Println("hello, world")
|
||||
if flag {
|
||||
//line fmthello.go:999999
|
||||
Println("bad line")
|
||||
for {
|
||||
}
|
||||
}
|
||||
|
@ -463,7 +463,7 @@ func goGcflags() string {
|
||||
}
|
||||
|
||||
func goGcflagsIsEmpty() bool {
|
||||
return "" == os.Getenv("GO_GCFLAGS")
|
||||
return "" == os.Getenv("GO_GCFLAGS")
|
||||
}
|
||||
|
||||
// run runs a test.
|
||||
|
Loading…
Reference in New Issue
Block a user