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(metrics): Parse custom units #1312

Merged
merged 7 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

**Bug Fixes**:

- Parse custom units with length < 15 without crashing. ([#1312](https://github.com/getsentry/relay/pull/1312))

## 22.6.0

**Compatibility:** This version of Relay requires Sentry server `22.6.0` or newer.
Expand Down
34 changes: 27 additions & 7 deletions relay-common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,29 +478,31 @@ impl fmt::Display for FractionUnit {
}
}

const CUSTOM_UNIT_MAX_SIZE: usize = 15;

/// Custom user-defined units without builtin conversion.
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct CustomUnit([u8; 15]);
pub struct CustomUnit([u8; CUSTOM_UNIT_MAX_SIZE]);

impl CustomUnit {
/// Parses a `CustomUnit` from a string.
pub fn parse(s: &str) -> Result<Self, ParseMetricUnitError> {
if s.len() > 15 || !s.is_ascii() {
if s.len() > CUSTOM_UNIT_MAX_SIZE || !s.is_ascii() {
return Err(ParseMetricUnitError(()));
}

let mut unit = Self(Default::default());
unit.0.copy_from_slice(s.as_bytes());
let mut unit = Self([0; CUSTOM_UNIT_MAX_SIZE]);
unit.0[..s.len()].copy_from_slice(s.as_bytes());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably try and avoid indexing operations, using .get() or so instead. IIRC it's also possible to add a clippy lint for this.

Copy link
Member

@untitaker untitaker Jun 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could use unit.0.get(..s.len()).ok_or(ParseMetricUnitError())? and forego the s.len() check on line 490 (but still leave the is_ascii check)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

unit.0.make_ascii_lowercase();
Ok(unit)
}

/// Returns the string representation of this unit.
#[inline]
pub fn as_str(&self) -> &str {
// Safety: The string is already validated to be of length 32 and valid ASCII when
// constructing `ProjectKey`.
unsafe { std::str::from_utf8_unchecked(&self.0) }
// Safety: The string is already validated to be valid ASCII when
// parsing `CustomUnit`.
unsafe { std::str::from_utf8_unchecked(&self.0).trim_end_matches('\0') }
}
}

Expand Down Expand Up @@ -628,3 +630,21 @@ impl schemars::JsonSchema for MetricUnit {
String::json_schema(gen)
}
}

#[cfg(test)]
mod tests {
use crate::CustomUnit;

#[test]
fn test_custom_unit_parse() {
assert_eq!("foo", CustomUnit::parse("Foo").unwrap().as_str());
assert_eq!(
"0123456789abcde",
CustomUnit::parse("0123456789abcde").unwrap().as_str()
);
assert!(matches!(
CustomUnit::parse("this_is_a_unit_that_is_too_long"),
Err(_)
));
}
}