diff --git a/src/capture.rs b/src/capture.rs index b22e0a3c..e408106b 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -5,9 +5,9 @@ use crate::PrintFmt; use crate::resolve; use crate::{BacktraceFmt, Symbol, SymbolName, resolve_frame, trace}; use core::ffi::c_void; -use std::fmt; use std::path::{Path, PathBuf}; use std::prelude::v1::*; +use std::{env, fmt}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -490,22 +490,21 @@ impl fmt::Debug for Backtrace { PrintFmt::Short }; - // When printing paths we try to strip the cwd if it exists, otherwise - // we just print the path as-is. Note that we also only do this for the // short format, because if it's full we presumably want to print // everything. - let cwd = std::env::current_dir(); let mut print_path = move |fmt: &mut fmt::Formatter<'_>, path: crate::BytesOrWideString<'_>| { let path = path.into_path_buf(); - if style != PrintFmt::Full { - if let Ok(cwd) = &cwd { - if let Ok(suffix) = path.strip_prefix(cwd) { - return fmt::Display::fmt(&suffix.display(), fmt); - } - } + // When printing paths we try to strip the cwd if it exists, otherwise + // we just print the path as-is + if style == PrintFmt::Short + && let Ok(cwd) = &env::current_dir() + && let Ok(suffix) = path.strip_prefix(cwd) + { + fmt::Display::fmt(&suffix.display(), fmt) + } else { + fmt::Display::fmt(&path.display(), fmt) } - fmt::Display::fmt(&path.display(), fmt) }; let mut f = BacktraceFmt::new(fmt, style, &mut print_path); diff --git a/src/symbolize/gimli.rs b/src/symbolize/gimli.rs index cbc0b768..b691bf2c 100644 --- a/src/symbolize/gimli.rs +++ b/src/symbolize/gimli.rs @@ -478,16 +478,16 @@ pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) } } if !any_frames { - if let Some((object_cx, object_addr)) = cx.object.search_object_map(addr as u64) { - if let Ok(mut frames) = object_cx.find_frames(stash, object_addr) { - while let Ok(Some(frame)) = frames.next() { - any_frames = true; - call(Symbol::Frame { - addr: addr as *mut c_void, - location: frame.location, - name: frame.function.map(|f| f.name.slice()), - }); - } + if let Some((object_cx, object_addr)) = cx.object.search_object_map(addr as u64) + && let Ok(mut frames) = object_cx.find_frames(stash, object_addr) + { + while let Ok(Some(frame)) = frames.next() { + any_frames = true; + call(Symbol::Frame { + addr: addr as *mut c_void, + location: frame.location, + name: frame.function.map(|f| f.name.slice()), + }); } } } diff --git a/src/symbolize/gimli/elf.rs b/src/symbolize/gimli/elf.rs index fd9903d1..646b8d2b 100644 --- a/src/symbolize/gimli/elf.rs +++ b/src/symbolize/gimli/elf.rs @@ -30,17 +30,17 @@ impl Mapping { let object = Object::parse(map)?; // Try to locate an external debug file using the build ID. - if let Some(path_debug) = object.build_id().and_then(locate_build_id) { - if let Some(mapping) = Mapping::new_debug(path, path_debug, None) { - return Some(Either::A(mapping)); - } + if let Some(path_debug) = object.build_id().and_then(locate_build_id) + && let Some(mapping) = Mapping::new_debug(path, path_debug, None) + { + return Some(Either::A(mapping)); } // Try to locate an external debug file using the GNU debug link section. - if let Some((path_debug, crc)) = object.gnu_debuglink_path(path) { - if let Some(mapping) = Mapping::new_debug(path, path_debug, Some(crc)) { - return Some(Either::A(mapping)); - } + if let Some((path_debug, crc)) = object.gnu_debuglink_path(path) + && let Some(mapping) = Mapping::new_debug(path, path_debug, Some(crc)) + { + return Some(Either::A(mapping)); } let dwp = Mapping::load_dwarf_package(path, stash); @@ -102,14 +102,14 @@ impl Mapping { // Try to locate a supplementary object file. let mut sup = None; - if let Some((path_sup, build_id_sup)) = object.gnu_debugaltlink_path(&path) { - if let Some(map_sup) = super::mmap(&path_sup) { - let map_sup = stash.cache_mmap(map_sup); - if let Some(sup_) = Object::parse(map_sup) { - if sup_.build_id() == Some(build_id_sup) { - sup = Some(sup_); - } - } + if let Some((path_sup, build_id_sup)) = object.gnu_debugaltlink_path(&path) + && let Some(map_sup) = super::mmap(&path_sup) + { + let map_sup = stash.cache_mmap(map_sup); + if let Some(sup_) = Object::parse(map_sup) + && sup_.build_id() == Some(build_id_sup) + { + sup = Some(sup_); } } @@ -532,10 +532,10 @@ pub(super) fn handle_split_dwarf<'data>( stash: &'data Stash, load: addr2line::SplitDwarfLoad>, ) -> Option>>> { - if let Some(dwp) = package.as_ref() { - if let Ok(Some(cu)) = dwp.find_cu(load.dwo_id, &load.parent) { - return Some(Arc::new(cu)); - } + if let Some(dwp) = package.as_ref() + && let Ok(Some(cu)) = dwp.find_cu(load.dwo_id, &load.parent) + { + return Some(Arc::new(cu)); } let mut path = PathBuf::new(); diff --git a/src/symbolize/gimli/macho.rs b/src/symbolize/gimli/macho.rs index 7112694d..06f6a1cc 100644 --- a/src/symbolize/gimli/macho.rs +++ b/src/symbolize/gimli/macho.rs @@ -34,12 +34,11 @@ impl Mapping { // contains and try to find a macho file which has a matching UUID as // the one of our own file. If we find a match that's the dwarf file we // want to return. - if let Some(uuid) = uuid { - if let Some(parent) = path.parent() { - if let Some(mapping) = Mapping::load_dsym(parent, uuid) { - return Some(mapping); - } - } + if let Some(uuid) = uuid + && let Some(parent) = path.parent() + && let Some(mapping) = Mapping::load_dsym(parent, uuid) + { + return Some(mapping); } // Looks like nothing matched our UUID, so let's at least return our own diff --git a/src/symbolize/mod.rs b/src/symbolize/mod.rs index d221ce80..06604fbf 100644 --- a/src/symbolize/mod.rs +++ b/src/symbolize/mod.rs @@ -376,10 +376,10 @@ impl<'a> fmt::Display for SymbolName<'a> { // This may fail to print if the demangled symbol isn't actually // valid, so handle the error here gracefully by not propagating // it outwards. - if let Some(ref cpp) = self.cpp_demangled.0 { - if let Ok(s) = cpp.demangle() { - return s.fmt(f); - } + if let Some(ref cpp) = self.cpp_demangled.0 + && let Ok(s) = cpp.demangle() + { + return s.fmt(f); } } @@ -398,10 +398,10 @@ impl<'a> fmt::Debug for SymbolName<'a> { // This may fail to print if the demangled symbol isn't actually // valid, so handle the error here gracefully by not propagating // it outwards. - if let Some(ref cpp) = self.cpp_demangled.0 { - if let Ok(s) = cpp.demangle() { - return s.fmt(f); - } + if let Some(ref cpp) = self.cpp_demangled.0 + && let Ok(s) = cpp.demangle() + { + return s.fmt(f); } } diff --git a/src/types.rs b/src/types.rs index c419247a..61f66c4a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -66,12 +66,15 @@ impl<'a> BytesOrWideString<'a> { } } - if let BytesOrWideString::Bytes(b) = self { - if let Ok(s) = str::from_utf8(b) { - return PathBuf::from(s); - } + // cross-platform fallback is to pray we are dealing with utf-8 + if let BytesOrWideString::Bytes(b) = self + && let Ok(s) = str::from_utf8(b) + { + PathBuf::from(s) + } else { + // or die + unreachable!() } - unreachable!() } }