Skip to content

Commit

Permalink
Fix timestamp-to-nanosecond conversion in the adapter (bytecodeallian…
Browse files Browse the repository at this point in the history
…ce#93)

The order of operations for handling the seconds/nanoseconds part of the
structure were a bit off which meant that a nonzero nanosecond field
would have an unexpected effect on the result.

Closes bytecodealliance#92
  • Loading branch information
alexcrichton authored Feb 22, 2023
1 parent 8c51e15 commit f807e25
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,9 @@ pub extern "C" fn clock_res_get(id: Clockid, resolution: &mut Timestamp) -> Errn
}
CLOCKID_REALTIME => {
let res = wasi_wall_clock::resolution(state.default_wall_clock());
*resolution = Timestamp::from(res.nanoseconds)
.checked_add(res.seconds)
.and_then(|secs| secs.checked_mul(1_000_000_000))
*resolution = Timestamp::from(res.seconds)
.checked_mul(1_000_000_000)
.and_then(|ns| ns.checked_add(res.nanoseconds.into()))
.ok_or(ERRNO_OVERFLOW)?;
}
_ => unreachable!(),
Expand Down Expand Up @@ -408,9 +408,9 @@ pub unsafe extern "C" fn clock_time_get(
}
CLOCKID_REALTIME => {
let res = wasi_wall_clock::now(state.default_wall_clock());
*time = Timestamp::from(res.nanoseconds)
.checked_add(res.seconds)
.and_then(|secs| secs.checked_mul(1_000_000_000))
*time = Timestamp::from(res.seconds)
.checked_mul(1_000_000_000)
.and_then(|ns| ns.checked_add(res.nanoseconds.into()))
.ok_or(ERRNO_OVERFLOW)?;
}
_ => unreachable!(),
Expand Down

0 comments on commit f807e25

Please sign in to comment.