Skip to content

Commit 063eaff

Browse files
authored
Fix potential panic (#57)
1 parent 5c2e072 commit 063eaff

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

src/threadstate.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,10 @@ impl ZoneInfo {
616616
use event_codes::*;
617617

618618
let Some(tinfo) = self.seen_threadsnaps.get(&event.thread_id) else {
619+
warn!(
620+
"no thread found for open tid={}: dropping event",
621+
event.thread_id
622+
);
619623
return Ok(());
620624
};
621625

@@ -941,7 +945,9 @@ impl ZoneInfo {
941945
let event_ts: u128 = event.timestamp.into();
942946
let thread_clone_ts: u128 = tinfo.clone_ts.into();
943947

944-
if event_ts - thread_clone_ts < Duration::new(2, 0).as_nanos() {
948+
// Use saturating_sub to prevent underflow if event_ts < thread_clone_ts
949+
// (which can happen if timestamps are from different time references)
950+
if event_ts.saturating_sub(thread_clone_ts) < Duration::new(2, 0).as_nanos() {
945951
//thread is valid and already in the table, nothing to do.
946952
// libsinsp: "Note that if we are in a container the caller
947953
// will never generate the child thread-info because it doesn't have
@@ -1471,7 +1477,10 @@ impl ZoneInfo {
14711477
fn parse_execve_exit(&mut self, event: &ZoneKernelSyscallEvent) -> Result<()> {
14721478
let Some(tinfo) = self.seen_threadsnaps.get_mut(&event.thread_id) else {
14731479
// if no thread info, bail
1474-
debug!("no thread found for execve: {:?}", event);
1480+
warn!(
1481+
"no thread found for execve tid={}: dropping event",
1482+
event.thread_id
1483+
);
14751484
return Ok(());
14761485
};
14771486

@@ -2001,7 +2010,10 @@ impl ZoneInfo {
20012010

20022011
// if no thread info, bail
20032012
if !self.seen_threadsnaps.contains_key(&event.thread_id) {
2004-
debug!("no thread found for fcntl event {:?}", event);
2013+
warn!(
2014+
"no thread found for close tid={}: dropping event",
2015+
event.thread_id
2016+
);
20052017
return Ok(());
20062018
}
20072019

0 commit comments

Comments
 (0)