Skip to content

Commit

Permalink
modfile: fix crash on AddGoStmt in empty File
Browse files Browse the repository at this point in the history
AddGoStmt uses File.Syntax without checking whether
it is nil or not. This causes crashes when using it on empty files that
have not had their Syntax member initialized to a valid pointer.

This change fixes it by ensuring File.Syntax is a valid pointer before
proceeding.

Fixes golang/go#62457.

Change-Id: Iab02039f79e73d939ca5d3e48b29faa5e0a9a5ec
Reviewed-on: https://go-review.googlesource.com/c/mod/+/570115
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
crazybolillo authored and gopherbot committed Mar 8, 2024
1 parent 766dc5d commit 18d3f56
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modfile/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,8 @@ func (f *File) AddGoStmt(version string) error {
var hint Expr
if f.Module != nil && f.Module.Syntax != nil {
hint = f.Module.Syntax
} else if f.Syntax == nil {
f.Syntax = new(FileSyntax)
}
f.Go = &Go{
Version: version,
Expand Down
40 changes: 40 additions & 0 deletions modfile/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,20 @@ var fixVersionTests = []struct {
},
}

var modifyEmptyFilesTests = []struct {
desc string
operations func(f *File)
want string
}{
{
desc: `addGoStmt`,
operations: func(f *File) {
f.AddGoStmt("1.20")
},
want: `go 1.20`,
},
}

func fixV(path, version string) (string, error) {
if path != "example.com/m" {
return "", fmt.Errorf("module path must be example.com/m")
Expand Down Expand Up @@ -1846,3 +1860,29 @@ func TestFixVersion(t *testing.T) {
})
}
}

func TestAddOnEmptyFile(t *testing.T) {
for _, tt := range modifyEmptyFilesTests {
t.Run(tt.desc, func(t *testing.T) {
f := &File{}
tt.operations(f)

expect, err := Parse("out", []byte(tt.want), nil)
if err != nil {
t.Fatal(err)
}
golden, err := expect.Format()
if err != nil {
t.Fatal(err)
}
got, err := f.Format()
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(got, golden) {
t.Fatalf("got:\n%s\nwant:\n%s", got, golden)
}
})
}
}

0 comments on commit 18d3f56

Please sign in to comment.