From a9ccd2d79574eead8c20d2bca4562cf2fd412787 Mon Sep 17 00:00:00 2001 From: Meng Zhuo Date: Sat, 26 Dec 2020 10:13:57 +0800 Subject: [PATCH] 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 Trust: Meng Zhuo Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Matthew Dempsky Reviewed-by: Russ Cox --- src/go/build/read.go | 35 +++++++++++++++++++++++++++++++++++ src/go/build/read_test.go | 20 ++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/go/build/read.go b/src/go/build/read.go index 6806a51c24..6da921d471 100644 --- a/src/go/build/read.go +++ b/src/go/build/read.go @@ -171,6 +171,41 @@ func (r *importReader) findEmbed(first bool) bool { case ' ', '\t': // leave startLine alone + case '"': + startLine = false + for r.err == nil { + if r.eof { + r.syntaxError() + } + c = r.readByteNoBuf() + if c == '\\' { + r.readByteNoBuf() + if r.err != nil { + r.syntaxError() + return false + } + continue + } + if c == '"' { + c = r.readByteNoBuf() + goto Reswitch + } + } + goto Reswitch + + case '`': + startLine = false + for r.err == nil { + if r.eof { + r.syntaxError() + } + c = r.readByteNoBuf() + if c == '`' { + c = r.readByteNoBuf() + goto Reswitch + } + } + case '/': c = r.readByteNoBuf() switch c { diff --git a/src/go/build/read_test.go b/src/go/build/read_test.go index 9264d2606f..36c773ecea 100644 --- a/src/go/build/read_test.go +++ b/src/go/build/read_test.go @@ -255,6 +255,26 @@ var readEmbedTests = []struct { "package p\nimport \"embed\"\n//go:embed x y z\nvar files embed.FS", []string{"x", "y", "z"}, }, + { + "package p\nimport \"embed\"\nvar s = \"/*\"\n//go:embed x\nvar files embed.FS", + []string{"x"}, + }, + { + `package p + import "embed" + var s = "\"\\\\" + //go:embed x + var files embed.FS`, + []string{"x"}, + }, + { + "package p\nimport \"embed\"\nvar s = `/*`\n//go:embed x\nvar files embed.FS", + []string{"x"}, + }, + { + "package p\nimport \"embed\"\nvar s = z/ *y\n//go:embed pointer\nvar pointer embed.FS", + []string{"pointer"}, + }, { "package p\n//go:embed x y z\n", // no import, no scan nil,