Skip to content

Commit 5a14e18

Browse files
felipecrsunknwon
andauthored
Fix parsing of double-quoted values with backslash continuations (#373)
Co-authored-by: ᴊᴏᴇ ᴄʜᴇɴ <jc@unknwon.io>
1 parent 54a0ff7 commit 5a14e18

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

ini_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,31 @@ key = test value <span style="color: %s\; background: %s">more text</span>
567567
assert.Equal(t, `test value <span style="color: %s; background: %s">more text</span>`, f.Section("").Key("key").String())
568568
})
569569

570+
t.Run("unescape double quotes with backslash continuation", func(t *testing.T) {
571+
f, err := LoadSources(LoadOptions{
572+
UnescapeValueDoubleQuotes: true,
573+
}, []byte(`hello = "!f() { \
574+
echo "hello world"; \
575+
};f"`))
576+
require.NoError(t, err)
577+
require.NotNil(t, f)
578+
579+
expected := `!f() { \
580+
echo "hello world"; \
581+
};f`
582+
assert.Equal(t, expected, f.Section("").Key("hello").String())
583+
584+
t.Run("inverse case", func(t *testing.T) {
585+
_, err := LoadSources(LoadOptions{
586+
UnescapeValueDoubleQuotes: true,
587+
IgnoreContinuation: true,
588+
}, []byte(`hello = "!f() { \
589+
echo "hello world"; \
590+
};f"`))
591+
require.Error(t, err)
592+
})
593+
})
594+
570595
t.Run("can parse small python-compatible INI files", func(t *testing.T) {
571596
f, err := LoadSources(LoadOptions{
572597
AllowPythonMultilineValues: true,

parser.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ func (p *parser) readMultilines(line, val, valQuote string) (string, error) {
182182

183183
pos := strings.LastIndex(next, valQuote)
184184
if pos > -1 {
185+
// Check if the line ends with backslash continuation after the quote
186+
restOfLine := strings.TrimRight(next[pos+len(valQuote):], "\r\n")
187+
if !p.options.IgnoreContinuation && strings.HasSuffix(strings.TrimSpace(restOfLine), `\`) {
188+
val += next
189+
continue
190+
}
191+
185192
val += next[:pos]
186193

187194
comment, has := cleanComment([]byte(next[pos:]))

0 commit comments

Comments
 (0)