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/text: gotext nil dereferences when a const declaration is in another file #60555

Open
mraron opened this issue Jun 1, 2023 · 1 comment
Open
Labels
help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mraron
Copy link

mraron commented Jun 1, 2023

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

$ go version
go version go1.20.4 linux/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="/home/aron/.cache/go-build"
GOENV="/home/aron/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/aron/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/aron/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/snap/go/current"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/snap/go/current/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="/usr/bin/gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/aron/text/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build922970473=/tmp/go-build -gno-record-gcc-switches"

What did you do?

login.go

package mvp

import (
	"golang.org/x/text/language"
	"golang.org/x/text/message"
)

const alreadyLoggedInMessage = "You're already logged in"

func LoginStuff() {
	message.NewPrinter(language.MustParse("hu-HU")).Sprintf(alreadyLoggedInMessage)
}

register.go

package mvp

import (
	"golang.org/x/text/language"
	"golang.org/x/text/message"
)

func RegisterStuff() {
	message.NewPrinter(language.MustParse("hu-HU")).Sprintf(alreadyLoggedInMessage)
}

Then I called gotext via gotext -srclang=en-US update -out=catalog.go -lang=en-US,hu-HU {package name}

I get the following panic:

"runtime error: invalid memory address or nil pointer dereference"
Stack:
	 4  0x00000000009975f9 in golang.org/x/text/message/pipeline.packageExtracter.handleCall
	     at /home/aron/text/message/pipeline/extract.go:600
	 5  0x0000000000996725 in golang.org/x/text/message/pipeline.(*extracter).extractMessages.func3
	     at /home/aron/text/message/pipeline/extract.go:545
	 6  0x000000000058791d in go/ast.inspector.Visit
	     at /snap/go/current/src/go/ast/walk.go:386
	 7  0x00000000005839ba in go/ast.Walk
	     at /snap/go/current/src/go/ast/walk.go:51
	 8  0x000000000058565b in go/ast.Walk
	     at /snap/go/current/src/go/ast/walk.go:206
	 9  0x0000000000583787 in go/ast.walkStmtList
	     at /snap/go/current/src/go/ast/walk.go:32
	10  0x0000000000585c53 in go/ast.Walk
	     at /snap/go/current/src/go/ast/walk.go:234
	11  0x000000000058703e in go/ast.Walk
	     at /snap/go/current/src/go/ast/walk.go:357
	12  0x00000000005838e7 in go/ast.walkDeclList
	     at /snap/go/current/src/go/ast/walk.go:38
	13  0x0000000000587169 in go/ast.Walk
	     at /snap/go/current/src/go/ast/walk.go:366
	14  0x00000000005879e5 in go/ast.Inspect
	     at /snap/go/current/src/go/ast/walk.go:397
	15  0x0000000000996427 in golang.org/x/text/message/pipeline.(*extracter).extractMessages
	     at /home/aron/text/message/pipeline/extract.go:542
	16  0x0000000000991c46 in golang.org/x/text/message/pipeline.Extract
	     at /home/aron/text/message/pipeline/extract.go:50
	17  0x00000000009aba9c in main.runUpdate
	     at /home/aron/text/cmd/gotext/update.go:35
	18  0x00000000009a90c3 in main.main
	     at /home/aron/text/cmd/gotext/main.go:149

As far as I understand it seems to happen because in extract.go ident.Obj is nil when extracting messages from register.go, since the the declaration is in the other file (according to #48141).

Indeed modifying the relevant code to something like:

		if obj := ident.Obj; obj != nil {
			if v, ok := obj.Decl.(*ast.ValueSpec); ok && v.Comment != nil {
				// TODO: get comment above ValueSpec as well
				comment = v.Comment.Text()
			}
		}

apparently fixes the problem.

What did you expect to see?

No panic.

What did you see instead?

A panic.

@gopherbot gopherbot added this to the Unreleased milestone Jun 1, 2023
@mknyszek mknyszek added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. help wanted labels Jun 1, 2023
chiahan1123 added a commit to chiahan1123/gotext that referenced this issue Sep 4, 2023
chiahan1123 added a commit to chiahan1123/gotext that referenced this issue Sep 4, 2023
@gopherbot
Copy link

Change https://go.dev/cl/545055 mentions this issue: x/text: fix nil dereference in gotext extract

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants