Fix parsing of double-quoted values with backslash continuations (#373)

Co-authored-by: ᴊᴏᴇ ᴄʜᴇɴ <jc@unknwon.io>
This commit is contained in:
Felipe Santos
2026-01-10 13:46:57 -05:00
committed by GitHub
co-authored by ᴊᴏᴇ ᴄʜᴇɴ
parent 54a0ff78ec
commit 5a14e1849d
2 changed files with 32 additions and 0 deletions
+25
View File
@@ -567,6 +567,31 @@ key = test value <span style="color: %s\; background: %s">more text</span>
assert.Equal(t, `test value <span style="color: %s; background: %s">more text</span>`, f.Section("").Key("key").String()) assert.Equal(t, `test value <span style="color: %s; background: %s">more text</span>`, f.Section("").Key("key").String())
}) })
t.Run("unescape double quotes with backslash continuation", func(t *testing.T) {
f, err := LoadSources(LoadOptions{
UnescapeValueDoubleQuotes: true,
}, []byte(`hello = "!f() { \
echo "hello world"; \
};f"`))
require.NoError(t, err)
require.NotNil(t, f)
expected := `!f() { \
echo "hello world"; \
};f`
assert.Equal(t, expected, f.Section("").Key("hello").String())
t.Run("inverse case", func(t *testing.T) {
_, err := LoadSources(LoadOptions{
UnescapeValueDoubleQuotes: true,
IgnoreContinuation: true,
}, []byte(`hello = "!f() { \
echo "hello world"; \
};f"`))
require.Error(t, err)
})
})
t.Run("can parse small python-compatible INI files", func(t *testing.T) { t.Run("can parse small python-compatible INI files", func(t *testing.T) {
f, err := LoadSources(LoadOptions{ f, err := LoadSources(LoadOptions{
AllowPythonMultilineValues: true, AllowPythonMultilineValues: true,
+7
View File
@@ -182,6 +182,13 @@ func (p *parser) readMultilines(line, val, valQuote string) (string, error) {
pos := strings.LastIndex(next, valQuote) pos := strings.LastIndex(next, valQuote)
if pos > -1 { if pos > -1 {
// Check if the line ends with backslash continuation after the quote
restOfLine := strings.TrimRight(next[pos+len(valQuote):], "\r\n")
if !p.options.IgnoreContinuation && strings.HasSuffix(strings.TrimSpace(restOfLine), `\`) {
val += next
continue
}
val += next[:pos] val += next[:pos]
comment, has := cleanComment([]byte(next[pos:])) comment, has := cleanComment([]byte(next[pos:]))