Skip to content

Commit 1ded833

Browse files
mdempskyrsc
authored andcommitted
[release-branch.go1.9] cmd/compile/internal/syntax: fix source buffer refilling
The previous code seems to have an off-by-1 in it somewhere, the consequence being that we didn't properly preserve all of the old buffer contents that we intended to. After spending a while looking at the existing window-shifting logic, I wasn't able to understand exactly how it was supposed to work or where the issue was, so I rewrote it to be (at least IMO) more obviously correct. Fixes #21938. Change-Id: I1ed7bbc1e1751a52ab5f7cf0411ae289586dc345 Reviewed-on: https://go-review.googlesource.com/64830 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-on: https://go-review.googlesource.com/70977 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent ff8289f commit 1ded833

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/cmd/compile/internal/syntax/scanner_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package syntax
77
import (
88
"fmt"
99
"os"
10+
"strings"
1011
"testing"
1112
)
1213

@@ -367,3 +368,15 @@ func TestScanErrors(t *testing.T) {
367368
}
368369
}
369370
}
371+
372+
func TestIssue21938(t *testing.T) {
373+
s := "/*" + strings.Repeat(" ", 4089) + "*/ .5"
374+
375+
var got scanner
376+
got.init(strings.NewReader(s), nil, nil)
377+
got.next()
378+
379+
if got.tok != _Literal || got.lit != ".5" {
380+
t.Errorf("got %s %q; want %s %q", got.tok, got.lit, _Literal, ".5")
381+
}
382+
}

src/cmd/compile/internal/syntax/source.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,12 @@ func (s *source) fill() {
164164
s.lit = append(s.lit, s.buf[s.suf:s.r0]...)
165165
s.suf = 1 // == s.r0 after slide below
166166
}
167-
s.offs += s.r0 - 1
168-
r := s.r - s.r0 + 1 // last read char plus one byte
169-
s.w = r + copy(s.buf[r:], s.buf[s.r:s.w])
170-
s.r = r
171-
s.r0 = 1
167+
n := s.r0 - 1
168+
copy(s.buf[:], s.buf[n:s.w])
169+
s.offs += n
170+
s.r0 = 1 // eqv: s.r0 -= n
171+
s.r -= n
172+
s.w -= n
172173
}
173174

174175
// read more data: try a limited number of times

0 commit comments

Comments
 (0)