From 2249a9c68e10db78172cf3d065e3b12c6ab62779 Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Sat, 12 Feb 2022 17:10:21 +0100 Subject: [PATCH] Multiline strings can't end with "\" i.e. this is valid: key = """ foo \ """ But this is not: key = """ foo \ """ Fixes #322 --- parse.go | 8 ++++++-- toml_test.go | 2 -- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/parse.go b/parse.go index 8269cca1..0420b5ba 100644 --- a/parse.go +++ b/parse.go @@ -220,7 +220,7 @@ func (p *parser) value(it item, parentIsArray bool) (interface{}, tomlType) { case itemString: return p.replaceEscapes(it, it.val), p.typeOfPrimitive(it) case itemMultilineString: - return p.replaceEscapes(it, stripFirstNewline(stripEscapedNewlines(it.val))), p.typeOfPrimitive(it) + return p.replaceEscapes(it, stripFirstNewline(p.stripEscapedNewlines(it.val))), p.typeOfPrimitive(it) case itemRawString: return it.val, p.typeOfPrimitive(it) case itemRawMultilineString: @@ -647,7 +647,7 @@ func stripFirstNewline(s string) string { } // Remove newlines inside triple-quoted strings if a line ends with "\". -func stripEscapedNewlines(s string) string { +func (p *parser) stripEscapedNewlines(s string) string { split := strings.Split(s, "\n") if len(split) < 1 { return s @@ -679,6 +679,10 @@ func stripEscapedNewlines(s string) string { continue } + if i == len(split)-1 { + p.panicf("invalid escape: '\\ '") + } + split[i] = line[:len(line)-1] // Remove \ if len(split)-1 > i { split[i+1] = strings.TrimLeft(split[i+1], " \t\r") diff --git a/toml_test.go b/toml_test.go index 8e923923..d79d751c 100644 --- a/toml_test.go +++ b/toml_test.go @@ -299,8 +299,6 @@ func TestToml(t *testing.T) { "invalid/inline-table/add", "invalid/table/duplicate-key-dotted-table", "invalid/table/duplicate-key-dotted-table2", - - "invalid/string/multiline-bad-escape-3", // https://github.com/BurntSushi/toml/issues/322 }, }