Skip to content

Commit

Permalink
Merge pull request #40 from killercup/feature/fuzz
Browse files Browse the repository at this point in the history
Add simple parser fuzzer
  • Loading branch information
LeopoldArkham authored Nov 27, 2017
2 parents fe00abf + 5ef7844 commit 015fc08
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

target
corpus
artifacts
22 changes: 22 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

[package]
name = "Molten-fuzz"
version = "0.0.1"
authors = ["Automatically generated"]
publish = false

[package.metadata]
cargo-fuzz = true

[dependencies.Molten]
path = ".."
[dependencies.libfuzzer-sys]
git = "https://github.com/rust-fuzz/libfuzzer-sys.git"

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[[bin]]
name = "parse"
path = "fuzz_targets/parse.rs"
9 changes: 9 additions & 0 deletions fuzz/fuzz_targets/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate Molten;

fuzz_target!(|data: &[u8]| {
if let Ok(text) = ::std::str::from_utf8(data) {
let _ = Molten::parser::Parser::new(text).parse();
}
});
16 changes: 14 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl<'a> Parser<'a> {
'"' => self.parse_basic_string(),
'\'' => self.parse_literal_string(),
// Boolean: true
't' if &self.src[self.idx..self.idx + 4] == "true" => {
't' if self.src[self.idx..].starts_with("true") => {
self.inc();
self.inc();
self.inc();
Expand All @@ -331,7 +331,7 @@ impl<'a> Parser<'a> {
})
}
// Boolean: False
'f' if &self.src[self.idx..self.idx + 5] == "false" => {
'f' if self.src[self.idx..].starts_with("false") => {
self.inc();
self.inc();
self.inc();
Expand Down Expand Up @@ -744,4 +744,16 @@ mod tests {
invalid_ints.iter().for_each(|s| {assert_eq!(None, Parser::parse_number(s, Trivia::default()))});
invalid_floats.iter().for_each(|s| {assert_eq!(None, Parser::parse_number(s, Trivia::default()))});
}

#[test]
fn issue41() {
let text = ::std::str::from_utf8(b"\'\'fb\'\xee\x9d\xbd").unwrap();
let _ = Parser::new(text).parse();
}

#[test]
fn issue42() {
let text = ::std::str::from_utf8(b"\'\nv\'f%\nb").unwrap();
let _ = Parser::new(text).parse();
}
}

0 comments on commit 015fc08

Please sign in to comment.