|
| 1 | +//! Test that the NFC iterator doesn't run needlessly further ahead of its |
| 2 | +//! underlying iterator. |
| 3 | +//! |
| 4 | +//! The NFC iterator is wrapped around the NFD iterator, and it buffers |
| 5 | +//! up combining characters so that it can sort them once it knows it has |
| 6 | +//! seen the complete sequence. At that point, it should drain its own |
| 7 | +//! buffer before consuming more characters from its inner iterator. |
| 8 | +//! This fuzz target defines a custom iterator which records how many |
| 9 | +//! times it's called so we can detect if NFC called it too many times. |
| 10 | +
|
| 11 | +#![no_main] |
| 12 | + |
| 13 | +#[macro_use] |
| 14 | +extern crate libfuzzer_sys; |
| 15 | + |
| 16 | +use std::str::Chars; |
| 17 | +use std::cell::RefCell; |
| 18 | +use std::rc::Rc; |
| 19 | +use unicode_normalization::UnicodeNormalization; |
| 20 | + |
| 21 | +const MAX_NONSTARTERS: u32 = 30; |
| 22 | + |
| 23 | +#[derive(Debug)] |
| 24 | +struct Counter<'a> { |
| 25 | + iter: Chars<'a>, |
| 26 | + value: Rc<RefCell<u32>>, |
| 27 | +} |
| 28 | + |
| 29 | +impl<'a> Iterator for Counter<'a> { |
| 30 | + type Item = char; |
| 31 | + |
| 32 | + fn next(&mut self) -> Option<char> { |
| 33 | + *self.value.borrow_mut() += 1; |
| 34 | + self.iter.next() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fuzz_target!(|input: String| { |
| 39 | + let stream_safe = input.chars().stream_safe().collect::<String>(); |
| 40 | + |
| 41 | + let mut value = Rc::new(RefCell::new(0)); |
| 42 | + let counter = Counter { iter: stream_safe.chars(), value: Rc::clone(&mut value) }; |
| 43 | + for _ in counter.nfc() { |
| 44 | + // Plus 2: one for the starter at the beginning of a sequence, and |
| 45 | + // one for a starter that begins the following sequence. |
| 46 | + assert!(*value.borrow() <= MAX_NONSTARTERS + 2); |
| 47 | + *value.borrow_mut() = 0; |
| 48 | + } |
| 49 | +}); |
0 commit comments