Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/go/ast/astutil: DeleteImport() causes SEGV #36383

Closed
itchyny opened this issue Jan 4, 2020 · 4 comments
Closed

x/tools/go/ast/astutil: DeleteImport() causes SEGV #36383

itchyny opened this issue Jan 4, 2020 · 4 comments

Comments

@itchyny
Copy link
Contributor

@itchyny itchyny commented Jan 4, 2020

What version of Go are you using (go version)?

$ go version
go version go1.13.5 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/itchyny/Library/Caches/go-build"
GOENV="/Users/itchyny/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/itchyny/.share/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.13.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.13.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/itchyny/Dropbox/go/gore/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/1c/r313z3nn77b3qyzyx6cnwcv80000gn/T/go-build404477944=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Run go run main.go where main.go is

package main

import (
	"fmt"
	"go/parser"
	"go/token"
	"log"

	"golang.org/x/tools/go/ast/astutil"
)

func main() {
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, "test.go", `package main

import "os"
`, parser.Mode(0))
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(astutil.AddImport(fset, file, "fmt"))
	fmt.Println(astutil.DeleteImport(fset, file, "fmt"))
}

What did you expect to see?

true
true

with no SEGV

What did you see instead?

true
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x10a0eed]

goroutine 1 [running]:
sync.(*Mutex).Lock(...)
        /usr/local/Cellar/go/1.13.5/libexec/src/sync/mutex.go:74
go/token.(*File).LineCount(0x0, 0x16)
        /usr/local/Cellar/go/1.13.5/libexec/src/go/token/position.go:125 +0x2d
golang.org/x/tools/go/ast/astutil.DeleteNamedImport(0xc0000ac040, 0xc0000b8000, 0x0, 0x0, 0x1105e11, 0x3, 0x0)
        /Users/itchyny/.share/go/src/golang.org/x/tools/go/ast/astutil/imports.go:282 +0x892
golang.org/x/tools/go/ast/astutil.DeleteImport(...)
        /Users/itchyny/.share/go/src/golang.org/x/tools/go/ast/astutil/imports.go:202
main.main()
        /private/tmp/main.go:22 +0x1ae
exit status 2
@gopherbot gopherbot added this to the Unreleased milestone Jan 4, 2020
@gopherbot gopherbot added the Tools label Jan 4, 2020
@toothrot

This comment has been minimized.

Copy link
Contributor

@toothrot toothrot commented Jan 7, 2020

I've investigated this for a bit, but I will defer to @dmitshur, who has much more experience with the astutil package.

@dmitshur

This comment has been minimized.

Copy link
Member

@dmitshur dmitshur commented Jan 7, 2020

Thanks for reporting.

This may be another edge case that isn't handled by CL 44372 nor CL 92250045.

Also /cc @fatih since he's also worked on this.

@itchyny

This comment has been minimized.

Copy link
Contributor Author

@itchyny itchyny commented Jan 11, 2020

golang/tools#196 should fix this.

@itchyny itchyny closed this Jan 18, 2020
@itchyny

This comment has been minimized.

Copy link
Contributor Author

@itchyny itchyny commented Jan 18, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.