Skip to content

Commit

Permalink
Add fuzz target for rustfmt crate
Browse files Browse the repository at this point in the history
  • Loading branch information
killercup committed Mar 13, 2017
1 parent d745b47 commit 50acbca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
members = [
"crypto-hashes",
"cssparser",
"httparse",
"flac",
"httparse",
"mp4parse",
"pulldown-cmark",
"quick-xml",
"regex",
"serde_json",
"tar",
"xml-rs",
Expand Down
12 changes: 12 additions & 0 deletions regex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "regex-targets"
version = "0.0.0"
publish = false

[dependencies]
regex = { git = "https://github.com/rust-lang-nursery/regex" }
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" }

[[bin]]
name = "is_match"
path = "is_match.rs"
21 changes: 21 additions & 0 deletions regex/is_match.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![no_main]

#[macro_use] extern crate libfuzzer_sys;
extern crate regex;

fuzz_target!(|data: &[u8]| {
if let Ok(data) = std::str::from_utf8(data) {
// split data into regular expression and actual input to search through
use std::cmp::max;
let len = data.chars().count();
let split_off_point = max(len / 5, 1) as usize;
let char_index = data.char_indices().nth(split_off_point);

if let Some((char_index, _)) = char_index {
let (pattern, input) = data.split_at(char_index);
if let Ok(re) = regex::Regex::new(pattern) {
re.is_match(input);
}
}
}
});

0 comments on commit 50acbca

Please sign in to comment.