|
| 1 | +// The fuzzing harness fuzz test some of the the |
| 2 | +// unicode string normalization processing |
| 3 | + |
| 4 | +#![no_main] |
| 5 | + |
| 6 | +#[macro_use] |
| 7 | +extern crate libfuzzer_sys; |
| 8 | +extern crate unicode_normalization; |
| 9 | + |
| 10 | +use unicode_normalization::{ |
| 11 | + char::{compose, canonical_combining_class, is_combining_mark, decompose_canonical, decompose_compatible}, |
| 12 | + UnicodeNormalization, |
| 13 | +}; |
| 14 | + |
| 15 | +fuzz_target!(|data: &[u8]| { |
| 16 | + let mut data = data; |
| 17 | + let c = if let Some((char_value, remaining_data)) = data.split_first() { |
| 18 | + match std::char::from_u32(*char_value as u32) { |
| 19 | + Some(ch) => { |
| 20 | + data = remaining_data; |
| 21 | + ch |
| 22 | + } |
| 23 | + None => return, |
| 24 | + } |
| 25 | + } else { |
| 26 | + return; |
| 27 | + }; |
| 28 | + |
| 29 | + // Generate second character for fuzzing if data is enough |
| 30 | + let c2 = if let Some((char_value, remaining_data)) = data.split_first() { |
| 31 | + data = remaining_data; |
| 32 | + std::char::from_u32(*char_value as u32) |
| 33 | + } else { |
| 34 | + None |
| 35 | + }; |
| 36 | + let string_data: String = data.iter().filter_map(|&b| std::char::from_u32(b as u32)).collect(); |
| 37 | + |
| 38 | + // Randomly choose a function target |
| 39 | + match data.first().map(|&b| b % 10) { |
| 40 | + Some(0) => { |
| 41 | + if let Some(c2) = c2 { |
| 42 | + let _ = compose(c, c2); |
| 43 | + } |
| 44 | + } |
| 45 | + Some(1) => { |
| 46 | + let _ = canonical_combining_class(c); |
| 47 | + } |
| 48 | + Some(2) => { |
| 49 | + let _ = is_combining_mark(c); |
| 50 | + } |
| 51 | + Some(3) => { |
| 52 | + let _ = string_data.nfc().collect::<String>(); |
| 53 | + } |
| 54 | + Some(4) => { |
| 55 | + let _ = string_data.nfkd().collect::<String>(); |
| 56 | + } |
| 57 | + Some(5) => { |
| 58 | + let _ = string_data.nfd().collect::<String>(); |
| 59 | + } |
| 60 | + Some(6) => { |
| 61 | + let _ = string_data.nfkc().collect::<String>(); |
| 62 | + } |
| 63 | + Some(7) => { |
| 64 | + let _ = string_data.stream_safe().collect::<String>(); |
| 65 | + } |
| 66 | + Some(8) => { |
| 67 | + decompose_canonical(c, |_| {}); |
| 68 | + } |
| 69 | + Some(9) => { |
| 70 | + decompose_compatible(c, |_| {}); |
| 71 | + } |
| 72 | + _ => {} |
| 73 | + } |
| 74 | +}); |
0 commit comments