Skip to content

Commit

Permalink
Move length overflow check to parser.rs and clarify doc
Browse files Browse the repository at this point in the history
  • Loading branch information
yescallop committed Apr 8, 2024
1 parent 05e8be7 commit ddcd45f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
13 changes: 0 additions & 13 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,12 @@ pub trait ToUri {
fn to_uri(self) -> Result<Uri<Self::Val>, Self::Err>;
}

#[cold]
fn len_overflow() -> ! {
panic!("input length > u32::MAX");
}

impl<'a> ToUri for &'a str {
type Val = &'a str;
type Err = ParseError;

#[inline]
fn to_uri(self) -> Result<Uri<Self::Val>, Self::Err> {
if self.len() > u32::MAX as usize {
len_overflow();
}

let meta = parser::parse(self.as_bytes())?;
Ok(Uri { val: self, meta })
}
Expand All @@ -45,10 +36,6 @@ impl ToUri for String {

#[inline]
fn to_uri(self) -> Result<Uri<Self::Val>, Self::Err> {
if self.len() > u32::MAX as usize {
len_overflow();
}

match parser::parse(self.as_bytes()) {
Ok(meta) => Ok(Uri { val: self, meta }),
Err(e) => Err(e.with_input(self)),
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl<T> Uri<T> {
/// - `Result<Uri<&str>, ParseError>` for `I = &str`;
/// - `Result<Uri<String>, ParseError<String>>` for `I = String`.
///
/// Returns `Ok` if and only if the string matches the [`URI-reference`]
/// ABNF rule from RFC 3986.
/// When not panicking, returns `Ok` if and only if the string matches
/// the [`URI-reference`] ABNF rule from RFC 3986.
///
/// You may recover an input [`String`] by calling [`ParseError::into_input`].
///
Expand Down
4 changes: 4 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ use core::{
type Result<T> = core::result::Result<T, ParseError>;

pub(crate) fn parse(bytes: &[u8]) -> Result<Meta> {
if bytes.len() > u32::MAX as usize {
panic!("input length > u32::MAX");
}

let mut parser = Parser {
reader: Reader::new(bytes),
out: Meta::default(),
Expand Down

0 comments on commit ddcd45f

Please sign in to comment.