Skip to content

Commit 2249a9c

Browse files
committed
Multiline strings can't end with "\"
i.e. this is valid: key = """ foo \ """ But this is not: key = """ foo \ """ Fixes #322
1 parent 51b22f2 commit 2249a9c

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

parse.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (p *parser) value(it item, parentIsArray bool) (interface{}, tomlType) {
220220
case itemString:
221221
return p.replaceEscapes(it, it.val), p.typeOfPrimitive(it)
222222
case itemMultilineString:
223-
return p.replaceEscapes(it, stripFirstNewline(stripEscapedNewlines(it.val))), p.typeOfPrimitive(it)
223+
return p.replaceEscapes(it, stripFirstNewline(p.stripEscapedNewlines(it.val))), p.typeOfPrimitive(it)
224224
case itemRawString:
225225
return it.val, p.typeOfPrimitive(it)
226226
case itemRawMultilineString:
@@ -647,7 +647,7 @@ func stripFirstNewline(s string) string {
647647
}
648648

649649
// Remove newlines inside triple-quoted strings if a line ends with "\".
650-
func stripEscapedNewlines(s string) string {
650+
func (p *parser) stripEscapedNewlines(s string) string {
651651
split := strings.Split(s, "\n")
652652
if len(split) < 1 {
653653
return s
@@ -679,6 +679,10 @@ func stripEscapedNewlines(s string) string {
679679
continue
680680
}
681681

682+
if i == len(split)-1 {
683+
p.panicf("invalid escape: '\\ '")
684+
}
685+
682686
split[i] = line[:len(line)-1] // Remove \
683687
if len(split)-1 > i {
684688
split[i+1] = strings.TrimLeft(split[i+1], " \t\r")

toml_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,6 @@ func TestToml(t *testing.T) {
299299
"invalid/inline-table/add",
300300
"invalid/table/duplicate-key-dotted-table",
301301
"invalid/table/duplicate-key-dotted-table2",
302-
303-
"invalid/string/multiline-bad-escape-3", // https://github.com/BurntSushi/toml/issues/322
304302
},
305303
}
306304

0 commit comments

Comments
 (0)