Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lite: tweak nest limit on stack overflow test #1106

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions regex-lite/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,24 @@ impl Hir {
}
}

impl HirKind {
/// Returns a slice of this kind's sub-expressions, if any.
fn subs(&self) -> &[Hir] {
use core::slice::from_ref;

match *self {
HirKind::Empty
| HirKind::Char(_)
| HirKind::Class(_)
| HirKind::Look(_) => &[],
HirKind::Repetition(Repetition { ref sub, .. }) => from_ref(sub),
HirKind::Capture(Capture { ref sub, .. }) => from_ref(sub),
HirKind::Concat(ref subs) => subs,
HirKind::Alternation(ref subs) => subs,
}
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct Class {
pub(crate) ranges: Vec<ClassRange>,
Expand Down Expand Up @@ -747,3 +765,45 @@ fn prev_char(ch: char) -> Option<char> {
// and U+E000 yields a valid scalar value.
Some(char::from_u32(u32::from(ch).checked_sub(1)?).unwrap())
}

impl Drop for Hir {
fn drop(&mut self) {
use core::mem;

match *self.kind() {
HirKind::Empty
| HirKind::Char(_)
| HirKind::Class(_)
| HirKind::Look(_) => return,
HirKind::Capture(ref x) if x.sub.kind.subs().is_empty() => return,
HirKind::Repetition(ref x) if x.sub.kind.subs().is_empty() => {
return
}
HirKind::Concat(ref x) if x.is_empty() => return,
HirKind::Alternation(ref x) if x.is_empty() => return,
_ => {}
}

let mut stack = vec![mem::replace(self, Hir::empty())];
while let Some(mut expr) = stack.pop() {
match expr.kind {
HirKind::Empty
| HirKind::Char(_)
| HirKind::Class(_)
| HirKind::Look(_) => {}
HirKind::Capture(ref mut x) => {
stack.push(mem::replace(&mut x.sub, Hir::empty()));
}
HirKind::Repetition(ref mut x) => {
stack.push(mem::replace(&mut x.sub, Hir::empty()));
}
HirKind::Concat(ref mut x) => {
stack.extend(x.drain(..));
}
HirKind::Alternation(ref mut x) => {
stack.extend(x.drain(..));
}
}
}
}
}
6 changes: 4 additions & 2 deletions regex-lite/src/hir/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,8 +1328,10 @@ fn into_class_item_range(hir: Hir) -> Result<char, Error> {
}
}

fn into_class_item_ranges(hir: Hir) -> Result<Vec<hir::ClassRange>, Error> {
match hir.kind {
fn into_class_item_ranges(
mut hir: Hir,
) -> Result<Vec<hir::ClassRange>, Error> {
match core::mem::replace(&mut hir.kind, HirKind::Empty) {
HirKind::Char(ch) => Ok(vec![hir::ClassRange { start: ch, end: ch }]),
HirKind::Class(hir::Class { ranges }) => Ok(ranges),
_ => Err(Error::new(ERR_CLASS_INVALID_ITEM)),
Expand Down
2 changes: 1 addition & 1 deletion regex-lite/tests/fuzz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn captures_wrong_order_min() {
#[test]
fn many_zero_to_many_reps() {
let pat = format!(".{}", "*".repeat(1 << 15));
let Ok(re) = regex_lite::RegexBuilder::new(&pat).build() else { return };
let Ok(re) = regex_lite::Regex::new(&pat) else { return };
re.is_match("");
}

Expand Down