From 1e21fac2cee92fdc2579f0efd5437f8733b242f0 Mon Sep 17 00:00:00 2001 From: sitandr <60141933+sitandr@users.noreply.github.com> Date: Tue, 8 Aug 2023 15:42:04 +0300 Subject: Fix crashing on empty regexps (#1870) --- crates/typst/src/model/selector.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/typst/src/model/selector.rs b/crates/typst/src/model/selector.rs index 9723ee4f..12f7fa0e 100644 --- a/crates/typst/src/model/selector.rs +++ b/crates/typst/src/model/selector.rs @@ -40,8 +40,22 @@ pub enum Selector { impl Selector { /// Define a simple text selector. - pub fn text(text: &str) -> Self { - Self::Regex(Regex::new(®ex::escape(text)).unwrap()) + pub fn text(text: &str) -> StrResult { + if text.is_empty() { + bail!("text selector is empty"); + } + Ok(Self::Regex(Regex::new(®ex::escape(text)).unwrap())) + } + + /// Define a regex selector. + pub fn regex(regex: Regex) -> StrResult { + if regex.as_str().is_empty() { + bail!("regex selector is empty"); + } + if regex.is_match("") { + bail!("regex matches empty text"); + } + Ok(Self::Regex(regex)) } /// Define a simple [`Selector::Can`] selector. @@ -158,8 +172,8 @@ cast! { .ok_or("only element functions can be used as selectors")? .select(), label: Label => Self::Label(label), - text: EcoString => Self::text(&text), - regex: Regex => Self::Regex(regex), + text: EcoString => Self::text(&text)?, + regex: Regex => Self::regex(regex)?, location: Location => Self::Location(location), } -- cgit v1.2.3