diff options
author | Matthew Dempsky <mdempsky@google.com> | 2017-09-19 14:28:03 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2017-10-25 20:23:03 +0000 |
commit | 1ded8334f7e99f6fd9284dd71f9fcf72da10c3c8 (patch) | |
tree | 200cda88886563d6f2940c225beec2367f265dfb | |
parent | ff8289f87936bd840daf6e704885f62399d01126 (diff) | |
download | go-1ded8334f7e99f6fd9284dd71f9fcf72da10c3c8.tar.gz go-1ded8334f7e99f6fd9284dd71f9fcf72da10c3c8.zip |
[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 <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-on: https://go-review.googlesource.com/70977
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
-rw-r--r-- | src/cmd/compile/internal/syntax/scanner_test.go | 13 | ||||
-rw-r--r-- | src/cmd/compile/internal/syntax/source.go | 11 |
2 files changed, 19 insertions, 5 deletions
diff --git a/src/cmd/compile/internal/syntax/scanner_test.go b/src/cmd/compile/internal/syntax/scanner_test.go index e434db9a91..53995e0c79 100644 --- a/src/cmd/compile/internal/syntax/scanner_test.go +++ b/src/cmd/compile/internal/syntax/scanner_test.go @@ -7,6 +7,7 @@ package syntax import ( "fmt" "os" + "strings" "testing" ) @@ -367,3 +368,15 @@ func TestScanErrors(t *testing.T) { } } } + +func TestIssue21938(t *testing.T) { + s := "/*" + strings.Repeat(" ", 4089) + "*/ .5" + + var got scanner + got.init(strings.NewReader(s), nil, nil) + got.next() + + if got.tok != _Literal || got.lit != ".5" { + t.Errorf("got %s %q; want %s %q", got.tok, got.lit, _Literal, ".5") + } +} diff --git a/src/cmd/compile/internal/syntax/source.go b/src/cmd/compile/internal/syntax/source.go index 93547213c0..4e3551225a 100644 --- a/src/cmd/compile/internal/syntax/source.go +++ b/src/cmd/compile/internal/syntax/source.go @@ -164,11 +164,12 @@ func (s *source) fill() { s.lit = append(s.lit, s.buf[s.suf:s.r0]...) s.suf = 1 // == s.r0 after slide below } - s.offs += s.r0 - 1 - r := s.r - s.r0 + 1 // last read char plus one byte - s.w = r + copy(s.buf[r:], s.buf[s.r:s.w]) - s.r = r - s.r0 = 1 + n := s.r0 - 1 + copy(s.buf[:], s.buf[n:s.w]) + s.offs += n + s.r0 = 1 // eqv: s.r0 -= n + s.r -= n + s.w -= n } // read more data: try a limited number of times |