From 0ee067db75d930b3a2564a7632890b5a0945efd7 Mon Sep 17 00:00:00 2001 From: YangKeao Date: Thu, 19 Jan 2023 16:23:36 +0800 Subject: [PATCH] validate whether addr is null before constructing slice Signed-off-by: YangKeao --- src/addr_validate.rs | 4 ++++ src/collector.rs | 2 +- src/frames.rs | 6 +++--- src/profiler.rs | 4 ++-- src/report.rs | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/addr_validate.rs b/src/addr_validate.rs index 9e9a34c6..4dd894e8 100644 --- a/src/addr_validate.rs +++ b/src/addr_validate.rs @@ -59,6 +59,10 @@ fn open_pipe() -> nix::Result<()> { } pub fn validate(addr: *const libc::c_void) -> bool { + if addr.is_null() { + return false; + } + const CHECK_LENGTH: usize = 2 * size_of::<*const libc::c_void>() / size_of::(); // read data in the pipe diff --git a/src/collector.rs b/src/collector.rs index 41b34139..c05fb18b 100644 --- a/src/collector.rs +++ b/src/collector.rs @@ -37,7 +37,7 @@ pub struct Bucket { impl Default for Bucket { fn default() -> Bucket { - let entries = Box::new(Default::default()); + let entries = Box::default(); Self { length: 0, entries } } diff --git a/src/frames.rs b/src/frames.rs index 94887585..31917443 100644 --- a/src/frames.rs +++ b/src/frames.rs @@ -18,7 +18,7 @@ pub struct UnresolvedFrames { pub frames: SmallVec<[::Frame; MAX_DEPTH]>, pub thread_name: [u8; MAX_THREAD_NAME], pub thread_name_length: usize, - pub thread_id: u64, + pub thread_id: libc::pthread_t, pub sample_timestamp: SystemTime, } @@ -45,7 +45,7 @@ impl UnresolvedFrames { pub fn new( frames: SmallVec<[::Frame; MAX_DEPTH]>, tn: &[u8], - thread_id: u64, + thread_id: libc::pthread_t, sample_timestamp: SystemTime, ) -> Self { let thread_name_length = tn.len(); @@ -168,7 +168,7 @@ impl Hash for Symbol { pub struct Frames { pub frames: Vec>, pub thread_name: String, - pub thread_id: u64, + pub thread_id: libc::pthread_t, pub sample_timestamp: SystemTime, } diff --git a/src/profiler.rs b/src/profiler.rs index 294b8c55..2301b936 100644 --- a/src/profiler.rs +++ b/src/profiler.rs @@ -358,7 +358,7 @@ extern "C" fn perf_signal_handler( write_thread_name(current_thread, &mut name); let name = unsafe { std::ffi::CStr::from_ptr(name_ptr) }; - profiler.sample(bt, name.to_bytes(), current_thread as u64, sample_timestamp); + profiler.sample(bt, name.to_bytes(), current_thread, sample_timestamp); } } } @@ -455,7 +455,7 @@ impl Profiler { &mut self, backtrace: SmallVec<[::Frame; MAX_DEPTH]>, thread_name: &[u8], - thread_id: u64, + thread_id: libc::pthread_t, sample_timestamp: SystemTime, ) { let frames = UnresolvedFrames::new(backtrace, thread_name, thread_id, sample_timestamp); diff --git a/src/report.rs b/src/report.rs index 37b885a8..971cb8d6 100644 --- a/src/report.rs +++ b/src/report.rs @@ -229,7 +229,7 @@ mod protobuf { /// `pprof` will generate google's pprof format report. pub fn pprof(&self) -> crate::Result { let mut dedup_str = HashSet::new(); - for key in self.data.iter().map(|(key, _)| key) { + for key in self.data.keys() { dedup_str.insert(key.thread_name_or_id()); for frame in key.frames.iter() { for symbol in frame {