fix: avoid panic when parsing with a multi-byte delimiter

Ini::parse split the value using delimiter + 1, assuming the delimiter
occupies a single byte. With a multi-byte UTF-8 delimiter this offset lands
inside the delimiter character, and slicing the string there panics. Advance
by the delimiter's actual UTF-8 length instead.
This commit is contained in:
dualfroz committed 2026-09-04 09:28:35 +02:00
1 parent eca6be7adc
commit 71e7f66418
3 files changed
+105 -1

No files matched your search

+16
View File
@@ -973,3 +973,19 @@ fn serde_case_sensitive_roundtrip() -> Result<(), Box<dyn Error>> {
Ok(())
}
#[test]
fn multibyte_delimiter_does_not_panic() -> Result<(), Box<dyn Error>> {
use configparser::ini::IniDefault;
// A multi-byte delimiter must not cause a panic when splitting the
// key from the value on a non-char-boundary byte offset.
let mut defaults = IniDefault::default();
defaults.delimiters = vec!['\u{00a7}']; // section sign, 2 bytes in UTF-8
let mut config = Ini::new_from_defaults(defaults);
config.read(String::from("[s]\nkey\u{00a7}value\n"))?;
assert_eq!(config.get("s", "key"), Some(String::from("value")));
Ok(())
}