From 87b210df6c4ee3cd823f3d0a4a6940084e765c82 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Fri, 11 Oct 2019 00:03:54 -0500 Subject: [PATCH] Fix sign when number of seconds is zero --- src/shims/time.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/shims/time.rs b/src/shims/time.rs index 97b74542d80..2995bdc4346 100644 --- a/src/shims/time.rs +++ b/src/shims/time.rs @@ -16,6 +16,7 @@ fn get_time() -> (Duration, i128) { impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {} pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> { + // Foreign function used by linux fn clock_gettime( &mut self, clk_id_op: OpTy<'tcx, Tag>, @@ -38,12 +39,17 @@ fn clock_gettime( let (duration, sign) = get_time(); let tv_sec = sign * (duration.as_secs() as i128); - let tv_nsec = duration.subsec_nanos() as i128; + let mut tv_nsec = duration.subsec_nanos() as i128; + // If the number of seconds is zero, we need to put the sign into the second's fraction. + if tv_sec == 0 && sign < 0 { + tv_nsec *= sign; + } + this.write_c_ints(&tp, &[tv_sec, tv_nsec], &["time_t", "c_long"])?; Ok(0) } - + // Foreign function used by generic unix fn gettimeofday( &mut self, tv_op: OpTy<'tcx, Tag>, @@ -66,7 +72,11 @@ fn gettimeofday( let (duration, sign) = get_time(); let tv_sec = sign * (duration.as_secs() as i128); - let tv_usec = duration.subsec_micros() as i128; + let mut tv_usec = duration.subsec_micros() as i128; + // If the number of seconds is zero, we need to put the sign into the second's fraction. + if tv_sec == 0 && sign < 0 { + tv_usec *= sign; + } this.write_c_ints(&tv, &[tv_sec, tv_usec], &["time_t", "suseconds_t"])?; -- 2.44.0