From fcaa1b0ec626ccc440c83a9dc2e97d78123fc0aa Mon Sep 17 00:00:00 2001 From: txtpbfmt team Date: Thu, 28 Apr 2022 07:11:23 -0700 Subject: [PATCH] Disallow empty value in list. $ echo "x [a, , b]" | blaze-bin/third_party/txtpbfmt/fmt parser.Format for path with content "x [a, , b]\n" returned err Empty value in list. F0428 14:10:56.453731 680717 fmt.go:103] 1 error(s) encountered during execution Note that this doesn't prevent empty strings: $ echo "x [a, '' , b]" | blaze-bin/third_party/txtpbfmt/fmt x [a, "", b] PiperOrigin-RevId: 445142675 --- parser/parser.go | 5 ++++- parser/parser_test.go | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/parser/parser.go b/parser/parser.go index 2920219..ad6f9a8 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -714,7 +714,10 @@ func (p *parser) parse(isRoot bool) (result []*ast.Node, endPos ast.Position, er return nil, ast.Position{}, err } if len(vals) != 1 { - return nil, ast.Position{}, fmt.Errorf("multiple-string value not supported (%v). Please add comma explcitily, see http://b/162070952", vals) + return nil, ast.Position{}, fmt.Errorf("multiple-string value not supported (%v). Please add comma explicitly, see http://b/162070952", vals) + } + if len(vals[0].Value) == 0 { + return nil, ast.Position{}, fmt.Errorf("empty value in list") } vals[0].PreComments = append(vals[0].PreComments, preComments...) diff --git a/parser/parser_test.go b/parser/parser_test.go index c3f8965..5390477 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -26,6 +26,8 @@ func TestError(t *testing.T) { }, { in: `name: "string with literal new line "`, err: "new line", + }, { + in: `list_with_empty_list: [a,,b]`, err: "empty value in list", }} for _, input := range inputs { out, err := Format([]byte(input.in))