Skip to content

Commit 8ffe5de

Browse files
committed
Fix sign when number of seconds is zero
1 parent fe81209 commit 8ffe5de

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/shims/time.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn get_time() -> (Duration, i128) {
1616

1717
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
1818
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
19+
// Foreign function used by linux
1920
fn clock_gettime(
2021
&mut self,
2122
clk_id_op: OpTy<'tcx, Tag>,
@@ -38,12 +39,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
3839

3940
let (duration, sign) = get_time();
4041
let tv_sec = sign * (duration.as_secs() as i128);
41-
let tv_nsec = duration.subsec_nanos() as i128;
42+
let mut tv_nsec = duration.subsec_nanos() as i128;
43+
// If the number of seconds is zero, we need to put the sign into the second's fraction.
44+
if tv_sec == 0 && sign < 0 {
45+
tv_nsec *= sign;
46+
}
47+
4248
this.write_c_ints(&tp, &[tv_sec, tv_nsec], &["time_t", "c_long"])?;
4349

4450
Ok(0)
4551
}
46-
52+
// Foreign function used by generic unix
4753
fn gettimeofday(
4854
&mut self,
4955
tv_op: OpTy<'tcx, Tag>,
@@ -66,7 +72,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6672

6773
let (duration, sign) = get_time();
6874
let tv_sec = sign * (duration.as_secs() as i128);
69-
let tv_usec = duration.subsec_micros() as i128;
75+
let mut tv_usec = duration.subsec_micros() as i128;
76+
// If the number of seconds is zero, we need to put the sign into the second's fraction.
77+
if tv_sec == 0 && sign < 0 {
78+
tv_usec *= sign;
79+
}
7080

7181
this.write_c_ints(&tv, &[tv_sec, tv_usec], &["time_t", "suseconds_t"])?;
7282

0 commit comments

Comments
 (0)