Skip to content

Commit

Permalink
Fix panic when character() is passed a string over 88 chars
Browse files Browse the repository at this point in the history
Closes GH-6.
  • Loading branch information
progval committed Aug 9, 2022
1 parent 498bbd7 commit 9404fb6
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ pub fn character(name: &str) -> Option<char> {
for (place, byte) in buf.iter_mut().zip(name.bytes()) {
*place = ASCII_UPPER_MAP[byte as usize]
}
let search_name = &buf[..name.len()];
let search_name = match buf.get(..name.len()) {
None => return None,
Some(search_name) => search_name,
};

// try `HANGUL SYLLABLE <choseong><jungseong><jongseong>`
if search_name.starts_with(HANGUL_SYLLABLE_PREFIX.as_bytes()) {
Expand Down Expand Up @@ -517,11 +520,15 @@ mod tests {

#[test]
fn character_negative() {
use MAX_NAME_LENGTH;
let long_name = "x".repeat(100);
assert!(long_name.len() > MAX_NAME_LENGTH); // Otherwise this test is pointless
let names = [
"",
"x",
"öäå",
"SPAACE"
"SPAACE",
&long_name,
];
for &n in names.iter() {
assert_eq!(character(n), None);
Expand Down

0 comments on commit 9404fb6

Please sign in to comment.