Skip to content

Commit a9ccd2d

Browse files
mengzhuorsc
authored andcommitted
go/build: skip string literal while findEmbed
The findEmbed function looking for comment by readbyte, however it might have constant or variables that contains comment. Maybe we should use ast parser in the future. Fixes #43373 Change-Id: I92544384fc4c11363d8b2f6b9898c8dea1602767 Reviewed-on: https://go-review.googlesource.com/c/go/+/280332 Trust: Matthew Dempsky <[email protected]> Trust: Meng Zhuo <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent d92f8ad commit a9ccd2d

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/go/build/read.go

+35
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,41 @@ func (r *importReader) findEmbed(first bool) bool {
171171
case ' ', '\t':
172172
// leave startLine alone
173173

174+
case '"':
175+
startLine = false
176+
for r.err == nil {
177+
if r.eof {
178+
r.syntaxError()
179+
}
180+
c = r.readByteNoBuf()
181+
if c == '\\' {
182+
r.readByteNoBuf()
183+
if r.err != nil {
184+
r.syntaxError()
185+
return false
186+
}
187+
continue
188+
}
189+
if c == '"' {
190+
c = r.readByteNoBuf()
191+
goto Reswitch
192+
}
193+
}
194+
goto Reswitch
195+
196+
case '`':
197+
startLine = false
198+
for r.err == nil {
199+
if r.eof {
200+
r.syntaxError()
201+
}
202+
c = r.readByteNoBuf()
203+
if c == '`' {
204+
c = r.readByteNoBuf()
205+
goto Reswitch
206+
}
207+
}
208+
174209
case '/':
175210
c = r.readByteNoBuf()
176211
switch c {

src/go/build/read_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,26 @@ var readEmbedTests = []struct {
255255
"package p\nimport \"embed\"\n//go:embed x y z\nvar files embed.FS",
256256
[]string{"x", "y", "z"},
257257
},
258+
{
259+
"package p\nimport \"embed\"\nvar s = \"/*\"\n//go:embed x\nvar files embed.FS",
260+
[]string{"x"},
261+
},
262+
{
263+
`package p
264+
import "embed"
265+
var s = "\"\\\\"
266+
//go:embed x
267+
var files embed.FS`,
268+
[]string{"x"},
269+
},
270+
{
271+
"package p\nimport \"embed\"\nvar s = `/*`\n//go:embed x\nvar files embed.FS",
272+
[]string{"x"},
273+
},
274+
{
275+
"package p\nimport \"embed\"\nvar s = z/ *y\n//go:embed pointer\nvar pointer embed.FS",
276+
[]string{"pointer"},
277+
},
258278
{
259279
"package p\n//go:embed x y z\n", // no import, no scan
260280
nil,

0 commit comments

Comments
 (0)