Skip to content

Commit

Permalink
panic safety
Browse files Browse the repository at this point in the history
  • Loading branch information
addisoncrump committed Jul 16, 2023
1 parent e5c1df3 commit dbb629d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
23 changes: 13 additions & 10 deletions fuzz/fuzz_targets/ast_diff_dense_dfa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_main]

use regex_automata::Input;
use {
libfuzzer_sys::{fuzz_target, Corpus},
regex_automata::{
Expand Down Expand Up @@ -42,16 +43,18 @@ fn do_fuzz(data: FuzzData) -> Corpus {
return Corpus::Reject;
};

assert_eq!(
re.is_match(&data.haystack),
baseline.is_match(&mut cache, &data.haystack)
);
let found1 = re.find(&data.haystack);
let found2 = baseline.find(&mut cache, &data.haystack);
if let Some(found1) = found1 {
let found2 = found2.expect("Found in target, but not in baseline!");
assert_eq!(found1.start(), found2.start());
assert_eq!(found1.end(), found2.end());
if let Ok(maybe_match) = re.try_search(&Input::new(&pattern)) {
assert_eq!(
maybe_match.is_some(),
baseline.is_match(&mut cache, &data.haystack)
);
let found2 = baseline.find(&mut cache, &data.haystack);
if let Some(found1) = maybe_match {
let found2 =
found2.expect("Found in target, but not in baseline!");
assert_eq!(found1.start(), found2.start());
assert_eq!(found1.end(), found2.end());
}
}

// no captures
Expand Down
25 changes: 15 additions & 10 deletions fuzz/fuzz_targets/ast_diff_hybrid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_main]

use regex_automata::Input;
use {
libfuzzer_sys::{fuzz_target, Corpus},
regex_automata::{
Expand Down Expand Up @@ -43,16 +44,20 @@ fn do_fuzz(data: FuzzData) -> Corpus {
};
let mut hybrid_cache = re.create_cache();

assert_eq!(
re.is_match(&mut hybrid_cache, &data.haystack),
baseline.is_match(&mut cache, &data.haystack)
);
let found1 = re.find(&mut hybrid_cache, &data.haystack);
let found2 = baseline.find(&mut cache, &data.haystack);
if let Some(found1) = found1 {
let found2 = found2.expect("Found in target, but not in baseline!");
assert_eq!(found1.start(), found2.start());
assert_eq!(found1.end(), found2.end());
if let Ok(maybe_match) =
re.try_search(&mut hybrid_cache, &Input::new(&pattern))
{
assert_eq!(
maybe_match.is_some(),
baseline.is_match(&mut cache, &data.haystack)
);
let found2 = baseline.find(&mut cache, &data.haystack);
if let Some(found1) = maybe_match {
let found2 =
found2.expect("Found in target, but not in baseline!");
assert_eq!(found1.start(), found2.start());
assert_eq!(found1.end(), found2.end());
}
}

// no captures
Expand Down
13 changes: 8 additions & 5 deletions fuzz/fuzz_targets/ast_diff_nfas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ fn do_fuzz(data: FuzzData) -> Corpus {
}
}
let mut backtracker_captures = backtracker.create_captures();
if let Ok(()) = backtracker.try_captures(
&mut backtracker_cache,
&data.haystack,
&mut backtracker_captures,
) {
if backtracker
.try_captures(
&mut backtracker_cache,
&data.haystack,
&mut backtracker_captures,
)
.is_ok()
{
let mut baseline_captures = baseline.create_captures();

baseline.captures(
Expand Down
23 changes: 13 additions & 10 deletions fuzz/fuzz_targets/ast_diff_sparse_dfa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_main]

use regex_automata::Input;
use {
libfuzzer_sys::{fuzz_target, Corpus},
regex_automata::{
Expand Down Expand Up @@ -46,16 +47,18 @@ fn do_fuzz(data: FuzzData) -> Corpus {
};
let re = RegexBuilder::new().build_from_dfas(fwd, rev);

assert_eq!(
re.is_match(&data.haystack),
baseline.is_match(&mut cache, &data.haystack)
);
let found1 = re.find(&data.haystack);
let found2 = baseline.find(&mut cache, &data.haystack);
if let Some(found1) = found1 {
let found2 = found2.expect("Found in target, but not in baseline!");
assert_eq!(found1.start(), found2.start());
assert_eq!(found1.end(), found2.end());
if let Ok(maybe_match) = re.try_search(&Input::new(&pattern)) {
assert_eq!(
maybe_match.is_some(),
baseline.is_match(&mut cache, &data.haystack)
);
let found2 = baseline.find(&mut cache, &data.haystack);
if let Some(found1) = maybe_match {
let found2 =
found2.expect("Found in target, but not in baseline!");
assert_eq!(found1.start(), found2.start());
assert_eq!(found1.end(), found2.end());
}
}

// no captures
Expand Down

0 comments on commit dbb629d

Please sign in to comment.