1
0
mirror of https://github.com/golang/go synced 2024-11-18 19:14:40 -07:00

internal/imports: fix setting default options

This fixes a nil pointer bug that occurs when opt is nil.

If nil is provide as opt to the internal/imports package, use the
default options settings.

Also check separately that Env is non-nil, as this would also cause
a crash.

Change-Id: I9a43b219b31ba80b7cb8111437f211cb72f1ca18
Reviewed-on: https://go-review.googlesource.com/c/tools/+/189939
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Suzy Mueller 2019-08-12 14:36:50 -04:00
parent f07d81a593
commit 4147ede4f8

View File

@ -108,17 +108,14 @@ func ApplyFixes(fixes []*ImportFix, filename string, src []byte, opt *Options) (
func initialize(filename string, src []byte, opt *Options) ([]byte, error) {
// Use defaults if opt is nil.
if opt == nil {
opt = &Options{
Env: &ProcessEnv{
GOPATH: build.Default.GOPATH,
GOROOT: build.Default.GOROOT,
},
AllErrors: opt.AllErrors,
Comments: opt.Comments,
FormatOnly: opt.FormatOnly,
Fragment: opt.Fragment,
TabIndent: opt.TabIndent,
TabWidth: opt.TabWidth,
opt = &Options{Comments: true, TabIndent: true, TabWidth: 8}
}
// Set the env if the user has not provided it.
if opt.Env == nil {
opt.Env = &ProcessEnv{
GOPATH: build.Default.GOPATH,
GOROOT: build.Default.GOROOT,
}
}