Skip to content

Commit

Permalink
fix: Avoid panicking on regex error in parse_grok() function
Browse files Browse the repository at this point in the history
Onig regex library, used by the grok library, panics when it hits
a retry (move to the start) limit. This catches the panic
and returns an error instead.

Ref: LOG-17425
  • Loading branch information
jorgebay committed Jun 30, 2023
1 parent f1a0ee8 commit 9a96163
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/stdlib/src/parse_grok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ static MEZMO_PATTERNS: &[(&str, &str)] = &[
mod non_wasm {
use ::value::Value;
pub(super) use std::sync::Arc;
use std::{collections::BTreeMap, fmt};
use std::{collections::BTreeMap, fmt, panic};
use vrl::prelude::*;
use vrl::state::TypeState;
use vrl_diagnostic::{Label, Span};

fn parse_grok(value: Value, pattern: Arc<grok::Pattern>) -> Resolved {
let bytes = value.try_bytes_utf8_lossy()?;
match pattern.match_against(&bytes) {

// Onig regex library, used by the grok library, panics when it hits a retry-limit-in-match.
// Fixing it in the grok library (by using another regex method) can be met
// with resistance because it requires a new API function, i.e., pattern.try_match_against()
let possible_panic = panic::catch_unwind(|| match pattern.match_against(&bytes) {
Some(matches) => {
let mut result = BTreeMap::new();

Expand All @@ -39,6 +43,15 @@ mod non_wasm {
Ok(Value::from(result))
}
None => Err("unable to parse input with grok pattern".into()),
});

match possible_panic {
Ok(r) => r,
Err(_) => Err(format!(
"regex with grok pattern caused a panic. Input: '{}', pattern: {:?}",
&bytes, pattern
)
.into()),
}
}

Expand Down

0 comments on commit 9a96163

Please sign in to comment.