From 681623f4e1a4b71474d0ba62c6296a938f7576fc Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 23 Jun 2015 21:34:40 -0700 Subject: [PATCH] hcl: don't allow nested comments This copies C's behavior and disallows nested block comments. It ignores a new /* within an existing block comment and ends at the first */ --- decoder_test.go | 8 ++++++++ hcl/lex.go | 7 +------ hcl/test-fixtures/comment.hcl | 1 - hcl/valuetype_string.go | 2 +- test-fixtures/nested_block_comment.hcl | 5 +++++ 5 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 test-fixtures/nested_block_comment.hcl diff --git a/decoder_test.go b/decoder_test.go index f4936ac..52397f1 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -188,6 +188,14 @@ func TestDecode_interface(t *testing.T) { }, }, }, + + { + "nested_block_comment.hcl", + false, + map[string]interface{}{ + "bar": "value", + }, + }, } for _, tc := range cases { diff --git a/hcl/lex.go b/hcl/lex.go index 825b7da..c5493ae 100644 --- a/hcl/lex.go +++ b/hcl/lex.go @@ -149,18 +149,13 @@ func (x *hclLex) consumeComment(c rune) bool { case '*': c = x.next() if c == '/' { - nested-- + return true } else { x.backup() } default: // Continue } - - // If we're done with the comment, return! - if nested == 0 { - return true - } } } diff --git a/hcl/test-fixtures/comment.hcl b/hcl/test-fixtures/comment.hcl index 8dfe815..1ff7f29 100644 --- a/hcl/test-fixtures/comment.hcl +++ b/hcl/test-fixtures/comment.hcl @@ -6,7 +6,6 @@ /* Baz */ -*/ # Another diff --git a/hcl/valuetype_string.go b/hcl/valuetype_string.go index efe119a..83b3904 100644 --- a/hcl/valuetype_string.go +++ b/hcl/valuetype_string.go @@ -9,7 +9,7 @@ const _ValueType_name = "ValueTypeUnknownValueTypeFloatValueTypeIntValueTypeStri var _ValueType_index = [...]uint8{0, 16, 30, 42, 57, 70, 82, 95, 110} func (i ValueType) String() string { - if i+1 >= ValueType(len(_ValueType_index)) { + if i >= ValueType(len(_ValueType_index)-1) { return fmt.Sprintf("ValueType(%d)", i) } return _ValueType_name[_ValueType_index[i]:_ValueType_index[i+1]] diff --git a/test-fixtures/nested_block_comment.hcl b/test-fixtures/nested_block_comment.hcl new file mode 100644 index 0000000..e827782 --- /dev/null +++ b/test-fixtures/nested_block_comment.hcl @@ -0,0 +1,5 @@ +/* +foo = "bar/*" +*/ + +bar = "value"