From 29b36a88ab0b179c140612e7c907042b2a388587 Mon Sep 17 00:00:00 2001 From: David Chase Date: Wed, 11 Mar 2020 13:36:42 -0400 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Cherry Zhang --- src/cmd/internal/objfile/disasm.go | 5 +++++ src/cmd/objdump/objdump_test.go | 13 ++++++++++--- src/cmd/objdump/testdata/fmthello.go | 2 ++ test/run.go | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/cmd/internal/objfile/disasm.go b/src/cmd/internal/objfile/disasm.go index b979a7f8aa0..35cfd35d37c 100644 --- a/src/cmd/internal/objfile/disasm.go +++ b/src/cmd/internal/objfile/disasm.go @@ -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 } diff --git a/src/cmd/objdump/objdump_test.go b/src/cmd/objdump/objdump_test.go index 0c2adbdb949..7ed32cf3c27 100644 --- a/src/cmd/objdump/objdump_test.go +++ b/src/cmd/objdump/objdump_test.go @@ -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) } diff --git a/src/cmd/objdump/testdata/fmthello.go b/src/cmd/objdump/testdata/fmthello.go index fd16ebee1b0..c8d82466dc9 100644 --- a/src/cmd/objdump/testdata/fmthello.go +++ b/src/cmd/objdump/testdata/fmthello.go @@ -5,6 +5,8 @@ import "fmt" func main() { Println("hello, world") if flag { +//line fmthello.go:999999 + Println("bad line") for { } } diff --git a/test/run.go b/test/run.go index 781c8d75ddc..bd63d7142bc 100644 --- a/test/run.go +++ b/test/run.go @@ -463,7 +463,7 @@ func goGcflags() string { } func goGcflagsIsEmpty() bool { - return "" == os.Getenv("GO_GCFLAGS") + return "" == os.Getenv("GO_GCFLAGS") } // run runs a test.