scanner: Report null bytes as errors, even at the end of file.

The formatter will append a newline at the end of file, causing the output
of printer.Format() to be invalid.
This commit is contained in:
Florian Forster 2018-03-20 12:10:43 +01:00 committed by Florian Forster
parent a81aa7b5dd
commit a5efd34964
2 changed files with 3 additions and 2 deletions

View File

@ -95,8 +95,7 @@ func (s *Scanner) next() rune {
s.srcPos.Column = 0
}
// If we see a null character with data left, then that is an error
if ch == '\x00' && s.buf.Len() > 0 {
if ch == '\x00' {
s.err("unexpected null character (0x00)")
return eof
}

View File

@ -512,6 +512,8 @@ func TestError(t *testing.T) {
testError(t, "ab\x80", "1:3", "illegal UTF-8 encoding", token.IDENT)
testError(t, "abc\xff", "1:4", "illegal UTF-8 encoding", token.IDENT)
testError(t, "ab\x00", "1:3", "unexpected null character (0x00)", token.IDENT)
testError(t, "ab\x00\n", "1:3", "unexpected null character (0x00)", token.IDENT)
testError(t, `"ab`+"\x80", "1:4", "illegal UTF-8 encoding", token.STRING)
testError(t, `"abc`+"\xff", "1:5", "illegal UTF-8 encoding", token.STRING)