Skip to content

Commit

Permalink
compiler: fix a byte class bug
Browse files Browse the repository at this point in the history
The code that computes the byte classes had a bug that was only tripped
when the maximum number of equivalence classes was necessary (256). This
commit fixes that by rewriting the loop such that we never uselessly
increment outside the bounds of `u8`.

Fixes rust-lang#367
  • Loading branch information
BurntSushi committed May 20, 2017
1 parent 68bc958 commit 1f0a025
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,11 +1033,16 @@ impl ByteClassSet {
// (0usize..256).map(|x| x as u8).collect()
let mut byte_classes = vec![0; 256];
let mut class = 0u8;
for i in 0..256 {
byte_classes[i] = class;
let mut i = 0;
loop {
byte_classes[i] = class as u8;
if i >= 255 {
break;
}
if self.0[i] {
class = class.checked_add(1).unwrap();
}
i += 1;
}
byte_classes
}
Expand Down Expand Up @@ -1084,4 +1089,13 @@ mod tests {
assert_eq!(classes[7], 3);
assert_eq!(classes[255], 3);
}

#[test]
fn full_byte_classes() {
let mut set = ByteClassSet::new();
for i in 0..256u16 {
set.set_range(i as u8, i as u8);
}
assert_eq!(set.byte_classes().len(), 256);
}
}

0 comments on commit 1f0a025

Please sign in to comment.