Skip to content

Skip the whole key-value delimiter when it is not ASCII - #456

Open
bardrop wants to merge 1 commit into
go-ini:mainfrom
bardrop-oss:fix-non-ascii-delimiter
Open

bardrop wants to merge 1 commit into
go-ini:mainfrom
bardrop-oss:fix-non-ascii-delimiter

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Describe the pull request

With a non-ASCII key-value delimiter, the value keeps the delimiter's trailing bytes:

f, _ := ini.Load([]byte("key1:value1\n"), ini.KeyValueDelimiters("=::"))
f.Section("").Key("key1").String() // "\xbc\x9avalue1", want "value1"

Same for a quoted key name ("key 2"→value2 gives "\x86\x92value2"). The value is not valid UTF-8 anymore.

Cause: readKeyName in lexer.go finds the delimiter with delimiters.index, then returns endIdx + 1 as the value offset (both the plain and the quoted-name branch). That is one byte, but : is 3 bytes in UTF-8. The rest of the parser already handles non-ASCII delimiters and comment symbols (charSet falls back to strings.IndexAny, the writer uses strings.ContainsAny), so only this offset was off. v1 has the same + 1.

Fix: move past the whole rune at the delimiter, using utf8.DecodeRuneInString. ASCII delimiters are unchanged. I added a line about it to the parsing fixes in docs/migrate-v1-to-v2.md.

Test: a new subtest in Test_KeyValueDelimiters loads key1:value1 and "key 2"→value2 with KeyValueDelimiters(":→"). It fails on main with the values above and passes with the change.

go test -race -count=1 ./..., go vet ./... and gofmt -l . pass locally (Go 1.26.5, macOS arm64). I also ran FuzzWriteRoundTrip for 30s and FuzzStructMapping for 20s with no failures. I did not run golangci-lint locally.

Link to the issue: n/a

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code.

AI usage

This PR was made with an AI coding agent (Claude): it found the bug while reading the lexer, wrote the fix and test, and ran the checks above. No human reviewed it before opening.

readKeyName moved past the delimiter by one byte, so a multi-byte
delimiter such as ":" left its other bytes at the start of the value.
@bardrop
bardrop requested a review from unknwon as a code owner November 26, 2026 06:31

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant