Skip to content

Commit a68867f

Browse files
Cap timeout at i64::MAX to prevent timespec overflow
1 parent e6d14b8 commit a68867f

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/uucore/src/lib/features/process.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,18 @@ impl ChildExt for Child {
159159
}
160160

161161
// Convert Duration to timespec for sigtimedwait
162-
let timeout_spec = libc::timespec {
163-
tv_sec: timeout.as_secs() as libc::time_t,
164-
tv_nsec: timeout.subsec_nanos() as libc::c_long,
162+
// Cap at i64::MAX to prevent overflow
163+
const MAX_TIMESPEC_SECONDS: u64 = i64::MAX as u64;
164+
let timeout_spec = if timeout.as_secs() >= MAX_TIMESPEC_SECONDS {
165+
libc::timespec {
166+
tv_sec: i64::MAX,
167+
tv_nsec: 0,
168+
}
169+
} else {
170+
libc::timespec {
171+
tv_sec: timeout.as_secs() as libc::time_t,
172+
tv_nsec: timeout.subsec_nanos() as libc::c_long,
173+
}
165174
};
166175

167176
// Wait for signals with timeout
@@ -259,12 +268,19 @@ impl ChildExt for Child {
259268
// Calculate timeout and deadline
260269
// Use checked_add to prevent overflow with very large timeouts
261270
let deadline = Instant::now().checked_add(timeout);
262-
// Cap timeout to avoid overflow in timespec conversion
263-
let timeout_secs = timeout.as_secs().min(libc::time_t::MAX as u64);
264-
let timeout_spec = Some(libc::timespec {
265-
tv_sec: timeout_secs as libc::time_t,
266-
tv_nsec: timeout.subsec_nanos() as libc::c_long,
267-
});
271+
// Cap timeout at i64::MAX to prevent overflow in timespec conversion
272+
const MAX_TIMESPEC_SECONDS: u64 = i64::MAX as u64;
273+
let timeout_spec = if timeout.as_secs() >= MAX_TIMESPEC_SECONDS {
274+
Some(libc::timespec {
275+
tv_sec: i64::MAX,
276+
tv_nsec: 0,
277+
})
278+
} else {
279+
Some(libc::timespec {
280+
tv_sec: timeout.as_secs() as libc::time_t,
281+
tv_nsec: timeout.subsec_nanos() as libc::c_long,
282+
})
283+
};
268284

269285
// Wait for signal events
270286
let mut eventlist = vec![KEvent::new(

0 commit comments

Comments
 (0)