aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateusz Poliwczak <mpoliwczak34@gmail.com>2026-01-23 09:52:03 +0100
committerAlan Donovan <adonovan@google.com>2026-01-26 07:58:30 -0800
commitf809faeb8e2534da29e980ebd98dfbd4e8a7f4ba (patch)
treed2b7616ac1b4b15b2a02fe13b99fbb92d1c9d599
parent1bd5dbfc4110740f2126e0253f429dfe5f3d04ac (diff)
downloadgo-f809faeb8e2534da29e980ebd98dfbd4e8a7f4ba.tar.xz
go/scanner: clear all fields after Scanner reuse
We were missing s.nlPos = token.NoPos in Init, but while we are here let's make it less likely to hit this it in future. Change-Id: Ief4c0ba2cf97bc556d901eabc8e172406a6a6964 Reviewed-on: https://go-review.googlesource.com/c/go/+/738680 Reviewed-by: Carlos Amedee <carlos@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com>
-rw-r--r--src/go/scanner/scanner.go22
-rw-r--r--src/go/scanner/scanner_test.go26
2 files changed, 37 insertions, 11 deletions
diff --git a/src/go/scanner/scanner.go b/src/go/scanner/scanner.go
index 07d987c88f..f8610640c0 100644
--- a/src/go/scanner/scanner.go
+++ b/src/go/scanner/scanner.go
@@ -144,18 +144,18 @@ func (s *Scanner) Init(file *token.File, src []byte, err ErrorHandler, mode Mode
if file.Size() != len(src) {
panic(fmt.Sprintf("file size (%d) does not match src len (%d)", file.Size(), len(src)))
}
- s.file = file
- s.dir, _ = filepath.Split(file.Name())
- s.src = src
- s.err = err
- s.mode = mode
- s.ch = ' '
- s.offset = 0
- s.rdOffset = 0
- s.lineOffset = 0
- s.insertSemi = false
- s.ErrorCount = 0
+ dir, _ := filepath.Split(file.Name())
+
+ *s = Scanner{
+ file: file,
+ dir: dir,
+ src: src,
+ err: err,
+ mode: mode,
+
+ ch: ' ',
+ }
s.next()
if s.ch == bom {
diff --git a/src/go/scanner/scanner_test.go b/src/go/scanner/scanner_test.go
index 98036bea4e..ae2cc63392 100644
--- a/src/go/scanner/scanner_test.go
+++ b/src/go/scanner/scanner_test.go
@@ -1153,3 +1153,29 @@ func TestNumbers(t *testing.T) {
}
}
}
+
+func TestScanReuseSemiInNewlineComment(t *testing.T) {
+ fset := token.NewFileSet()
+
+ const src = "identifier /*a\nb*/ + other"
+ var s Scanner
+ s.Init(fset.AddFile("test.go", -1, len(src)), []byte(src), func(pos token.Position, msg string) {
+ t.Fatal(msg)
+ }, ScanComments)
+
+ s.Scan() // IDENT(identifier)
+
+ _, tok, _ := s.Scan() // COMMENT(/*a\nb*/)
+ if tok != token.COMMENT {
+ t.Fatalf("tok = %v; want = token.SEMICOLON", tok)
+ }
+
+ s.Init(fset.AddFile("test.go", -1, len(src)), []byte(src), func(pos token.Position, msg string) {
+ t.Fatal(msg)
+ }, ScanComments)
+
+ _, tok, _ = s.Scan()
+ if tok != token.IDENT {
+ t.Fatalf("tok = %v; want = token.IDENT", tok)
+ }
+}