]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/objdump: guard against out-of-range lines from directives.
authorDavid Chase <drchase@google.com>
Wed, 11 Mar 2020 17:36:42 +0000 (13:36 -0400)
committerDavid Chase <drchase@google.com>
Thu, 12 Mar 2020 20:40:54 +0000 (20:40 +0000)
//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>
src/cmd/internal/objfile/disasm.go
src/cmd/objdump/objdump_test.go
src/cmd/objdump/testdata/fmthello.go
test/run.go

index b979a7f8aa0df2dd1cb5d3ea6cfa933e58aa06b0..35cfd35d37c83d7331d128d18b3d61bac009409a 100644 (file)
@@ -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
 }
 
index 0c2adbdb94909c20e6676228841d932b1c0f99d0..7ed32cf3c2737b0659c657f8aaad333cbfca7592 100644 (file)
@@ -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)
        }
index fd16ebee1b0bf2c546026be315a780b12458ac69..c8d82466dc9d75850006ee9fada06c0a45fbc2c6 100644 (file)
@@ -5,6 +5,8 @@ import "fmt"
 func main() {
        Println("hello, world")
        if flag {
+//line fmthello.go:999999
+               Println("bad line")
                for {
                }
        }
index 781c8d75ddce14c86a69841fa6db5270cdf5f659..bd63d7142bc507a8fc2289fa62ad13daebc9cd48 100644 (file)
@@ -463,7 +463,7 @@ func goGcflags() string {
 }
 
 func goGcflagsIsEmpty() bool {
-       return "" == os.Getenv("GO_GCFLAGS")
+       return "" == os.Getenv("GO_GCFLAGS")
 }
 
 // run runs a test.