Skip to content

Commit

Permalink
Merge pull request #81 from nygrenh/parse-str-panic
Browse files Browse the repository at this point in the history
Fix an index out of bounds panic in parse_str
  • Loading branch information
alexcrichton authored Mar 6, 2017
2 parents bc7f509 + 32ec3be commit 906f3e5
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,11 @@ impl Uuid {
let mut buffer = [0u8; 16];

for (i_char, chr) in input.chars().enumerate() {
if digit as usize >= SIMPLE_LENGTH && group == 0 {
return Err(ParseError::InvalidLength(len));
if digit as usize >= SIMPLE_LENGTH && group != 4 {
if group == 0 {
return Err(ParseError::InvalidLength(len));
}
return Err(ParseError::InvalidGroups(group + 1));
}

if digit % 2 == 0 {
Expand Down Expand Up @@ -661,9 +664,7 @@ impl Uuid {
}

// Now check the last group.
if group != 0 && group != 4 {
return Err(ParseError::InvalidGroups(group + 1));
} else if ACC_GROUP_LENS[4] != digit {
if ACC_GROUP_LENS[4] != digit {
return Err(ParseError::InvalidGroupLength(group,
(digit - ACC_GROUP_LENS[3]) as usize,
GROUP_LENS[4]));
Expand Down Expand Up @@ -925,6 +926,10 @@ mod tests {
Err(InvalidLength(35)));
assert_eq!(Uuid::parse_str("F9168C5E-CEB2-4faa-BGBF-329BF39FA1E4"),
Err(InvalidCharacter('G', 20)));
assert_eq!(Uuid::parse_str("F9168C5E-CEB2F4faaFB6BFF329BF39FA1E4"),
Err(InvalidGroups(2)));
assert_eq!(Uuid::parse_str("F9168C5E-CEB2-4faaFB6BFF329BF39FA1E4"),
Err(InvalidGroups(3)));
assert_eq!(Uuid::parse_str("F9168C5E-CEB2-4faa-B6BFF329BF39FA1E4"),
Err(InvalidGroups(4)));
assert_eq!(Uuid::parse_str("F9168C5E-CEB2-4faa"),
Expand Down

0 comments on commit 906f3e5

Please sign in to comment.