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

fix(pattern): Handle empty alternates #4277

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Terminate the process when one of the services crashes. ([#4249](https://github.com/getsentry/relay/pull/4249))
- Don't propagate trace sampling decisions from SDKs ([#4265](https://github.com/getsentry/relay/pull/4265))
- Rate limit profile chunks. ([#4270](https://github.com/getsentry/relay/pull/4270))
- Correctly handle empty alternates in patterns. ([#4277](https://github.com/getsentry/relay/pull/4277))

**Features**:

Expand Down
71 changes: 46 additions & 25 deletions relay-pattern/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ struct Parser<'a> {
tokens: Tokens,
alternates: Option<Vec<Tokens>>,
current_literal: Option<String>,
current_alternate: Option<Tokens>,
options: Options,
complexity: u64,
}
Expand All @@ -489,6 +490,7 @@ impl<'a> Parser<'a> {
tokens: Default::default(),
alternates: None,
current_literal: None,
current_alternate: None,
options,
complexity: 0,
}
Expand All @@ -508,10 +510,11 @@ impl<'a> Parser<'a> {
None => return Err(ErrorKind::DanglingEscape),
},
',' if self.alternates.is_some() => {
self.finish_literal();
// safe to unwrap, we just checked for `some`.
let alternates = self.alternates.as_mut().unwrap();
alternates.push(Tokens::default());
// If we encounter a ',' and we don't have a `current_alternate`,
// we have to start one to match the empty string.
self.current_alternate.get_or_insert_with(Tokens::default);
self.finish_alternate();
self.current_alternate = Some(Tokens::default());
}
c => self.push_literal(c),
}
Expand All @@ -533,21 +536,33 @@ impl<'a> Parser<'a> {
}

fn end_alternates(&mut self) -> Result<(), ErrorKind> {
self.finish_alternate();
let Some(alternates) = self.alternates.take() else {
return Err(ErrorKind::UnbalancedAlternates);
};
if !alternates.is_empty() {
self.complexity = self
.complexity
.max(1)
.saturating_mul(alternates.len() as u64);
self.push_token(Token::Alternates(alternates));
}

Ok(())
}

fn finish_alternate(&mut self) {
self.finish_literal();
match self.alternates.take() {
None => return Err(ErrorKind::UnbalancedAlternates),
Some(alternates) => {
if !alternates.is_empty() {
self.complexity = self
.complexity
.max(1)
.saturating_mul(alternates.len() as u64);
self.push_token(Token::Alternates(alternates));
if let Some(alternates) = self.alternates.as_mut() {
if let Some(mut tokens) = self.current_alternate.take() {
// If the current alternate is empty, replace it with "".
// An empty alternate should match the empty string.
if tokens.is_empty() {
tokens.push(Token::Literal(Literal::new(String::new(), self.options)))
}
alternates.push(tokens);
}
}

Ok(())
}

fn parse_class(&mut self) -> Result<(), ErrorKind> {
Expand Down Expand Up @@ -626,16 +641,12 @@ impl<'a> Parser<'a> {
/// Pushes the passed `token` and finishes the currently in progress literal token.
fn push_token(&mut self, token: Token) {
self.finish_literal();
match self.alternates.as_mut() {
Some(alternates) => match alternates.last_mut() {
Some(tokens) => tokens.push(token),
None => {
let mut tokens = Tokens::default();
tokens.push(token);
alternates.push(tokens);
}
},
None => self.tokens.push(token),
if self.alternates.is_some() {
self.current_alternate
.get_or_insert_with(Tokens::default)
.push(token);
} else {
self.tokens.push(token)
}
}

Expand Down Expand Up @@ -716,6 +727,10 @@ impl Tokens {
fn as_slice(&self) -> &[Token] {
self.0.as_slice()
}

fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

/// Represents a token in a Relay pattern.
Expand Down Expand Up @@ -1473,6 +1488,12 @@ mod tests {
assert_pattern!("{foo,abc}{def,bar}", NOT "fooabc");
assert_pattern!("{foo,abc}{def,bar}", NOT "defdef");
assert_pattern!("{foo,abc}{def,bar}", NOT "defabc");
assert_pattern!("foo{,/}", "foo");
assert_pattern!("foo{/,}", "foo");
assert_pattern!("foo{,/}", "foo/");
assert_pattern!("foo{/,}", "foo/");
assert_pattern!("{,}", "");
assert_pattern!("{}", NOT "");
Dav1dde marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
Expand Down
Loading