Skip to content

Marginally improve v0 error granularity and testing. #54

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

Merged
merged 7 commits into from
Jul 26, 2021
115 changes: 80 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@
#[macro_use]
extern crate std;

// HACK(eddyb) helper macros for tests.
#[cfg(test)]
macro_rules! assert_contains {
($s:expr, $needle:expr) => {{
let (s, needle) = ($s, $needle);
assert!(
s.contains(needle),
"{:?} should've contained {:?}",
s,
needle
);
}};
}
#[cfg(test)]
macro_rules! assert_ends_with {
($s:expr, $suffix:expr) => {{
let (s, suffix) = ($s, $suffix);
assert!(
s.ends_with(suffix),
"{:?} should've ended in {:?}",
s,
suffix
);
}};
}

mod legacy;
mod v0;

Expand Down Expand Up @@ -90,7 +116,12 @@ pub fn demangle(mut s: &str) -> Demangle {
suffix = s;
Some(DemangleStyle::V0(d))
}
Err(v0::Invalid) => None,
// FIXME(eddyb) would it make sense to treat an unknown-validity
// symbol (e.g. one that errored with `RecursedTooDeep`) as
// v0-mangled, and have the error show up in the demangling?
// (that error already gets past this initial check, and therefore
// will show up in the demangling, if hidden behind a backref)
Err(v0::ParseError::Invalid) | Err(v0::ParseError::RecursedTooDeep) => None,
},
};

Expand Down Expand Up @@ -188,37 +219,55 @@ impl<'a> fmt::Display for DemangleStyle<'a> {
// Maximum size of the symbol that we'll print.
const MAX_SIZE: usize = 1_000_000;

struct LimitedFmtWriter<F> {
remaining: usize,
#[derive(Copy, Clone, Debug)]
struct SizeLimitExhausted;

struct SizeLimitedFmtAdapter<F> {
remaining: Result<usize, SizeLimitExhausted>,
inner: F,
}

impl<F: fmt::Write> fmt::Write for LimitedFmtWriter<F> {
impl<F: fmt::Write> fmt::Write for SizeLimitedFmtAdapter<F> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let remaining = self.remaining.checked_sub(s.len());
self.remaining = remaining.unwrap_or(0);
self.remaining = self
.remaining
.and_then(|r| r.checked_sub(s.len()).ok_or(SizeLimitExhausted));

match remaining {
Some(_) => self.inner.write_str(s),
None => Err(fmt::Error),
match self.remaining {
Ok(_) => self.inner.write_str(s),
Err(SizeLimitExhausted) => Err(fmt::Error),
}
}
}

impl<'a> fmt::Display for Demangle<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let alternate = f.alternate();
let mut f = LimitedFmtWriter {
remaining: MAX_SIZE,
inner: f,
};
match self.style {
None => f.write_str(self.original)?,
Some(ref d) => {
if alternate {
write!(f, "{:#}", d)?;
let alternate = f.alternate();
let mut size_limited_fmt = SizeLimitedFmtAdapter {
remaining: Ok(MAX_SIZE),
inner: &mut *f,
};
let fmt_result = if alternate {
write!(size_limited_fmt, "{:#}", d)
} else {
write!(f, "{}", d)?;
write!(size_limited_fmt, "{}", d)
};
let size_limit_result = size_limited_fmt.remaining.map(|_| ());

// Translate a `fmt::Error` generated by `SizeLimitedFmtAdapter`
// into an error message, instead of propagating it upwards
// (which could cause panicking from inside e.g. `std::io::print`).
match (fmt_result, size_limit_result) {
(Err(_), Err(SizeLimitExhausted)) => f.write_str("{size limit reached}")?,

_ => {
fmt_result?;
size_limit_result
.expect("`fmt::Error` from `SizeLimitedFmtAdapter` was discarded");
}
}
}
}
Expand Down Expand Up @@ -418,31 +467,27 @@ mod tests {

#[test]
fn limit_recursion() {
// NOTE(eddyb) the `?` indicate that a parse error was encountered.
// FIXME(eddyb) replace `v0::Invalid` with a proper `v0::ParseError`,
// that could show e.g. `<recursion limit reached>` instead of `?`.
assert_eq!(
super::demangle("_RNvB_1a").to_string().replace("::a", ""),
"?"
assert_contains!(
super::demangle("_RNvB_1a").to_string(),
"{recursion limit reached}"
);
assert_eq!(
super::demangle("_RMC0RB2_").to_string().replace("&", ""),
"<?>"
assert_contains!(
super::demangle("_RMC0RB2_").to_string(),
"{recursion limit reached}"
);
}

#[test]
fn limit_output() {
use std::fmt::Write;
let mut s = String::new();
assert!(write!(
s,
"{}",
super::demangle("RYFG_FGyyEvRYFF_EvRYFFEvERLB_B_B_ERLRjB_B_B_")
)
.is_err());
assert_ends_with!(
super::demangle("RYFG_FGyyEvRYFF_EvRYFFEvERLB_B_B_ERLRjB_B_B_").to_string(),
"{size limit reached}"
);
// NOTE(eddyb) somewhat reduced version of the above, effectively
// `<for<...> fn()>` with a larger number of lifetimes in `...`.
assert!(write!(s, "{}", super::demangle("_RMC0FGZZZ_Eu")).is_err());
assert_ends_with!(
super::demangle("_RMC0FGZZZ_Eu").to_string(),
"{size limit reached}"
);
}
}
Loading