Skip to content

Commit

Permalink
more cold, faster length tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Feb 24, 2021
1 parent 359c854 commit 9ee9091
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 12 additions & 0 deletions crates/proto/src/error.rs
Expand Up @@ -271,18 +271,21 @@ impl From<ProtoErrorKind> for ProtoError {
}

impl From<&'static str> for ProtoError {
#[cold]
fn from(msg: &'static str) -> ProtoError {
ProtoErrorKind::Message(msg).into()
}
}

impl From<String> for ProtoError {
#[cold]
fn from(msg: String) -> ProtoError {
ProtoErrorKind::Msg(msg).into()
}
}

impl From<io::Error> for ProtoError {
#[cold]
fn from(e: io::Error) -> ProtoError {
match e.kind() {
io::ErrorKind::TimedOut => ProtoErrorKind::Timeout.into(),
Expand All @@ -292,36 +295,42 @@ impl From<io::Error> for ProtoError {
}

impl<T> From<sync::PoisonError<T>> for ProtoError {
#[cold]
fn from(_e: sync::PoisonError<T>) -> ProtoError {
ProtoErrorKind::Poisoned.into()
}
}

impl From<Unspecified> for ProtoError {
#[cold]
fn from(e: Unspecified) -> ProtoError {
ProtoErrorKind::from(e).into()
}
}

impl From<SslErrorStack> for ProtoError {
#[cold]
fn from(e: SslErrorStack) -> ProtoError {
ProtoErrorKind::from(e).into()
}
}

impl From<url::ParseError> for ProtoError {
#[cold]
fn from(e: url::ParseError) -> ProtoError {
ProtoErrorKind::from(e).into()
}
}

impl From<std::str::Utf8Error> for ProtoError {
#[cold]
fn from(e: std::str::Utf8Error) -> ProtoError {
ProtoErrorKind::from(e).into()
}
}

impl From<std::num::ParseIntError> for ProtoError {
#[cold]
fn from(e: std::num::ParseIntError) -> ProtoError {
ProtoErrorKind::from(e).into()
}
Expand Down Expand Up @@ -372,6 +381,7 @@ pub mod not_ring {
}

impl From<ProtoError> for io::Error {
#[cold]
fn from(e: ProtoError) -> Self {
match *e.kind() {
ProtoErrorKind::Timeout => io::Error::new(io::ErrorKind::TimedOut, e),
Expand All @@ -381,13 +391,15 @@ impl From<ProtoError> for io::Error {
}

impl From<ProtoError> for String {
#[cold]
fn from(e: ProtoError) -> Self {
e.to_string()
}
}

#[cfg(feature = "wasm-bindgen")]
impl From<ProtoError> for wasm_bindgen_crate::JsValue {
#[cold]
fn from(e: ProtoError) -> Self {
js_sys::Error::new(&e.to_string()).into()
}
Expand Down
4 changes: 3 additions & 1 deletion crates/proto/src/rr/domain/name.rs
Expand Up @@ -1059,6 +1059,7 @@ fn read_inner(
) -> ProtoResult<()> {
let mut state: LabelParseState = LabelParseState::LabelLengthOrPointer;
let name_start = decoder.index();
let mut run_len: usize = labels.iter().map(Label::len).sum();

// assume all chars are utf-8. We're doing byte-by-byte operations, no endianess issues...
// reserved: (1000 0000 aka 0800) && (0100 0000 aka 0400)
Expand All @@ -1078,7 +1079,7 @@ fn read_inner(
}

// enforce max length of name
let cur_len = labels.iter().map(|l| l.len()).sum::<usize>() + labels.len();
let cur_len = run_len + labels.len();
if cur_len > 255 {
return Err(ProtoErrorKind::DomainNameTooLong(cur_len).into());
}
Expand All @@ -1103,6 +1104,7 @@ fn read_inner(
.verify_unwrap(|l| l.len() <= 63)
.map_err(|_| ProtoError::from("label exceeds maximum length of 63"))?;

run_len += label.len();
labels.push(Label::from_raw_bytes(label).unwrap());

// reset to collect more data
Expand Down

0 comments on commit 9ee9091

Please sign in to comment.