Skip to content

Commit

Permalink
scanner: Report null bytes as errors, even at the end of file.
Browse files Browse the repository at this point in the history
The formatter will append a newline at the end of file, causing the output
of printer.Format() to be invalid.
  • Loading branch information
octo committed Mar 20, 2018
1 parent a3dd1c8 commit 3c90585
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 1 addition & 2 deletions hcl/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions hcl/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3c90585

Please sign in to comment.