Skip to content

Commit adef769

Browse files
authored
Merge pull request #241 from octo/scanner-null
printer, scanner: Don't produce unparsable output.
2 parents b1738d9 + ec2ba18 commit adef769

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

hcl/printer/printer_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,25 @@ func lineAt(text []byte, offs int) []byte {
147147
}
148148
return text[offs:i]
149149
}
150+
151+
// TestFormatParsable ensures that the output of Format() is can be parsed again.
152+
func TestFormatValidOutput(t *testing.T) {
153+
cases := []string{
154+
"#\x00",
155+
"#\ue123t",
156+
}
157+
158+
for _, c := range cases {
159+
f, err := Format([]byte(c))
160+
if err != nil {
161+
// ignore these failures, not all inputs are valid HCL.
162+
t.Logf("Format(%q) = %v", c, err)
163+
continue
164+
}
165+
166+
if _, err := parser.Parse(f); err != nil {
167+
t.Errorf("Format(%q) = %q; Parse(%q) = %v", c, f, f, err)
168+
continue
169+
}
170+
}
171+
}

hcl/scanner/scanner.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,16 @@ func (s *Scanner) next() rune {
9595
s.srcPos.Column = 0
9696
}
9797

98-
// If we see a null character with data left, then that is an error
99-
if ch == '\x00' && s.buf.Len() > 0 {
98+
if ch == '\x00' {
10099
s.err("unexpected null character (0x00)")
101100
return eof
102101
}
103102

103+
if ch == '\uE123' {
104+
s.err("unicode code point U+E123 reserved for internal use")
105+
return utf8.RuneError
106+
}
107+
104108
// debug
105109
// fmt.Printf("ch: %q, offset:column: %d:%d\n", ch, s.srcPos.Offset, s.srcPos.Column)
106110
return ch

hcl/scanner/scanner_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,12 @@ func TestScan_crlf(t *testing.T) {
509509
func TestError(t *testing.T) {
510510
testError(t, "\x80", "1:1", "illegal UTF-8 encoding", token.ILLEGAL)
511511
testError(t, "\xff", "1:1", "illegal UTF-8 encoding", token.ILLEGAL)
512+
testError(t, "\uE123", "1:1", "unicode code point U+E123 reserved for internal use", token.ILLEGAL)
512513

513514
testError(t, "ab\x80", "1:3", "illegal UTF-8 encoding", token.IDENT)
514515
testError(t, "abc\xff", "1:4", "illegal UTF-8 encoding", token.IDENT)
516+
testError(t, "ab\x00", "1:3", "unexpected null character (0x00)", token.IDENT)
517+
testError(t, "ab\x00\n", "1:3", "unexpected null character (0x00)", token.IDENT)
515518

516519
testError(t, `"ab`+"\x80", "1:4", "illegal UTF-8 encoding", token.STRING)
517520
testError(t, `"abc`+"\xff", "1:5", "illegal UTF-8 encoding", token.STRING)

0 commit comments

Comments
 (0)