Skip to content

Commit 42f038d

Browse files
drakkangopherbot
authored andcommitted
x/text: fix nil dereference in gotext extract
Ident.Obj may be nil if the referenced declaration is in another file. Fixes golang/go#60555 Change-Id: I730af89c7f52540b305b3da8c19448d089e9744c Reviewed-on: https://go-review.googlesource.com/c/text/+/545055 Reviewed-by: Emmanuel Odeke <[email protected]> Reviewed-by: Junyang Shao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Sean Liao <[email protected]> Auto-Submit: Emmanuel Odeke <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent a42f0e2 commit 42f038d

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

message/pipeline/extract.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,13 @@ func (px packageExtracter) handleCall(call *ast.CallExpr) bool {
597597
key := []string{}
598598
if ident, ok := format.(*ast.Ident); ok {
599599
key = append(key, ident.Name)
600-
if v, ok := ident.Obj.Decl.(*ast.ValueSpec); ok && v.Comment != nil {
601-
// TODO: get comment above ValueSpec as well
602-
comment = v.Comment.Text()
600+
// Ident.Obj may be nil if the referenced declaration is in another
601+
// file.
602+
if ident.Obj != nil {
603+
if v, ok := ident.Obj.Decl.(*ast.ValueSpec); ok && v.Comment != nil {
604+
// TODO: get comment above ValueSpec as well
605+
comment = v.Comment.Text()
606+
}
603607
}
604608
}
605609
if c := px.getComment(call.Args[0]); c != "" {

message/pipeline/testdata/test60555/catalog_gen.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"language": "en-US",
3+
"messages": [
4+
{
5+
"id": [
6+
"testMessage",
7+
"Test message"
8+
],
9+
"key": "Test message",
10+
"message": "Test message",
11+
"translation": "",
12+
"position": "testdata/test60555/main.go:17:10"
13+
}
14+
]
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"golang.org/x/text/language"
9+
"golang.org/x/text/message"
10+
)
11+
12+
// Ensures that we can catch any regressions with nil dereferences
13+
// from const declarations in other files within the same package.
14+
// See issue https://golang.org/issues/60555
15+
func main() {
16+
p := message.NewPrinter(language.English)
17+
p.Printf(testMessage)
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
const testMessage = "Test message"

0 commit comments

Comments
 (0)