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:
1 parent
8cb9cc4c65
commit
602b3965ae
2 files changed
+22
-1
No files matched your search
+7
-1
@@ -1017,7 +1017,13 @@ impl Ini {
|
|||||||
current_key = Some(key.clone());
|
current_key = Some(key.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
let value = trimmed[delimiter + 1..].trim().to_owned();
|
// `delimiter` is a byte offset and may itself be multi-byte, so advance by
|
||||||
|
// its UTF-8 length.
|
||||||
|
let delimiter_len = trimmed[delimiter..]
|
||||||
|
.chars()
|
||||||
|
.next()
|
||||||
|
.map_or(1, char::len_utf8);
|
||||||
|
let value = trimmed[delimiter + delimiter_len..].trim().to_owned();
|
||||||
|
|
||||||
valmap.insert(key, Some(value));
|
valmap.insert(key, Some(value));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -973,3 +973,18 @@ fn serde_case_sensitive_roundtrip() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn multibyte_delimiter_does_not_panic() -> Result<(), Box<dyn Error>> {
|
||||||
|
use configparser::ini::IniDefault;
|
||||||
|
|
||||||
|
// A multi-byte delimiter must not split on a non-char boundary.
|
||||||
|
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(())
|
||||||
|
}
|
||||||
Reference in new issue
Block a user