From 21c00906a39a7c8b5be5bc698591b508ae8c7f8d Mon Sep 17 00:00:00 2001 From: Luc Talatinian <102624213+lucix-aws@users.noreply.github.com> Date: Thu, 16 Nov 2023 11:22:05 -0500 Subject: [PATCH] fix trailing comment parse in properties (#2371) --- .../db2095be68314e47882d4a8881cc72ff.json | 8 ++++++++ internal/ini/parse.go | 2 +- internal/ini/strings.go | 20 ++++++++++++++----- internal/ini/testdata/valid/comments | 16 +++++++++++++++ internal/ini/testdata/valid/comments_expected | 19 ++++++++++++++++++ internal/ini/tokenize.go | 5 +++-- 6 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 .changelog/db2095be68314e47882d4a8881cc72ff.json create mode 100644 internal/ini/testdata/valid/comments create mode 100644 internal/ini/testdata/valid/comments_expected diff --git a/.changelog/db2095be68314e47882d4a8881cc72ff.json b/.changelog/db2095be68314e47882d4a8881cc72ff.json new file mode 100644 index 00000000000..641c66b3352 --- /dev/null +++ b/.changelog/db2095be68314e47882d4a8881cc72ff.json @@ -0,0 +1,8 @@ +{ + "id": "db2095be-6831-4e47-882d-4a8881cc72ff", + "type": "bugfix", + "description": "Fix recognition of trailing comments in shared config properties. # or ; separators that aren't preceded by whitespace at the end of a property value should be considered part of it.", + "modules": [ + "internal/ini" + ] +} \ No newline at end of file diff --git a/internal/ini/parse.go b/internal/ini/parse.go index 0fcb8ec0763..2422d90461b 100644 --- a/internal/ini/parse.go +++ b/internal/ini/parse.go @@ -82,7 +82,7 @@ func (p *parser) handleSubProperty(tok *lineTokenSubProperty) { // "promote" this to a normal property. p.handleProperty(&lineTokenProperty{ Key: tok.Key, - Value: strings.TrimSpace(trimComment(tok.Value)), + Value: strings.TrimSpace(trimPropertyComment(tok.Value)), }) return } diff --git a/internal/ini/strings.go b/internal/ini/strings.go index 478239a2505..661588c2276 100644 --- a/internal/ini/strings.go +++ b/internal/ini/strings.go @@ -1,11 +1,21 @@ package ini -import "strings" +import ( + "strings" +) -func trimComment(v string) string { - rest, _, _ := strings.Cut(v, "#") - rest, _, _ = strings.Cut(rest, ";") - return rest +func trimProfileComment(s string) string { + r, _, _ := strings.Cut(s, "#") + r, _, _ = strings.Cut(r, ";") + return r +} + +func trimPropertyComment(s string) string { + r, _, _ := strings.Cut(s, " #") + r, _, _ = strings.Cut(r, " ;") + r, _, _ = strings.Cut(r, "\t#") + r, _, _ = strings.Cut(r, "\t;") + return r } // assumes no surrounding comment diff --git a/internal/ini/testdata/valid/comments b/internal/ini/testdata/valid/comments new file mode 100644 index 00000000000..32ce942d524 --- /dev/null +++ b/internal/ini/testdata/valid/comments @@ -0,0 +1,16 @@ +[profile comments];comment +a = foo#notcomment +b = foo;notcomment +c = foo# +d = foo; +e = foo bar # comment +f = foo bar ; comment +g = foo # comment +h = foo ; comment +i = # comment +j = ; comment +k = foo # ; comment +l = foo ; # comment +m = foo +n = foo # +o = foo ; diff --git a/internal/ini/testdata/valid/comments_expected b/internal/ini/testdata/valid/comments_expected new file mode 100644 index 00000000000..67efd602b76 --- /dev/null +++ b/internal/ini/testdata/valid/comments_expected @@ -0,0 +1,19 @@ +{ + "profile comments": { + "a": "foo#notcomment", + "b": "foo;notcomment", + "c": "foo#", + "d": "foo;", + "e": "foo bar", + "f": "foo \t bar", + "g": "foo", + "h": "foo", + "i": "", + "j": "", + "k": "foo", + "l": "foo", + "m": "foo", + "n": "foo", + "o": "foo" + } +} diff --git a/internal/ini/tokenize.go b/internal/ini/tokenize.go index 9778a1738b3..89a7736841e 100644 --- a/internal/ini/tokenize.go +++ b/internal/ini/tokenize.go @@ -30,7 +30,7 @@ func isLineComment(line string) bool { } func asProfile(line string) *lineTokenProfile { // " [ type name ] ; comment" - trimmed := strings.TrimSpace(trimComment(line)) // "[ type name ]" + trimmed := strings.TrimSpace(trimProfileComment(line)) // "[ type name ]" if !isBracketed(trimmed) { return nil } @@ -48,7 +48,8 @@ func asProperty(line string) *lineTokenProperty { return nil } - trimmed := strings.TrimRight(trimComment(line), " \t") + trimmed := trimPropertyComment(line) + trimmed = strings.TrimRight(trimmed, " \t") k, v, ok := splitProperty(trimmed) if !ok { return nil