Skip to content

fix(vmm/sandbox): avoid intermediate overflow in checked_compute_delta - #249

Open
thanhtoantnt wants to merge 1 commit into
kuasar-io:mainfrom
thanhtoantnt:fixed_244
Open

fix(vmm/sandbox): avoid intermediate overflow in checked_compute_delta#249
thanhtoantnt wants to merge 1 commit into
kuasar-io:mainfrom
thanhtoantnt:fixed_244

Conversation

@thanhtoantnt

Copy link
Copy Markdown
Contributor

The intermediate sum delta_client + delta_server could overflow i64 even when the final result (sum / 2) fits in i64. For example:
checked_compute_delta(i64::MAX, 0, 0, i64::MAX)
should return Ok(i64::MAX) but previously returned Err due to overflow.

Fix: widen both operands to i128 before adding. The sum of two i64 values always fits in i128, and after dividing by 2 the result always fits back in i64, so the try_from cast is infallible in practice.

Also add three regression tests covering the i64::MAX, i64::MIN, and subtraction-overflow cases.

Fixes #244

@thanhtoantnt
thanhtoantnt requested a review from a team as a code owner May 20, 2026 14:30
Copilot AI review requested due to automatic review settings May 20, 2026 14:30

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies the checked_compute_delta function in vmm/sandbox/src/client.rs to prevent intermediate integer overflows by widening i64 values to i128 during calculation. It also adds several unit tests to verify correct behavior with extreme values like i64::MAX and i64::MIN. The review feedback suggests simplifying the code by using a direct cast to i64 instead of try_from, as the result is mathematically guaranteed to fit within the i64 range, and removing redundant parentheses for better readability.

Comment thread vmm/sandbox/src/client.rs Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an integer overflow bug in checked_compute_delta used during host/guest clock synchronization by widening intermediate arithmetic so large timestamp deltas no longer fail when the final averaged result still fits in i64 (Fixes #244).

Changes:

  • Compute delta_client + delta_server in i128 to prevent intermediate i64 overflow before dividing by 2.
  • Keep subtraction overflow protection via checked_sub, while making the final conversion back to i64 explicit via try_from.
  • Add regression tests covering i64::MAX, i64::MIN, and a subtraction-overflow error case.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@thanhtoantnt

Copy link
Copy Markdown
Contributor Author

@novahe Could you review this fix?

@novahe

novahe commented May 21, 2026

Copy link
Copy Markdown
Contributor

@novahe Could you review this fix?

thanks! nit: please squash commits

The intermediate sum delta_client + delta_server could overflow i64
even when the final result (sum / 2) fits in i64. For example:
  checked_compute_delta(i64::MAX, 0, 0, i64::MAX)
should return Ok(i64::MAX) but previously returned Err due to overflow.

Fix: widen both operands to i128 before adding. The sum of two i64
values always fits in i128, and after dividing by 2 the result is
guaranteed to fit back in i64, so a direct as i64 cast is used.

Also add three regression tests covering the i64::MAX, i64::MIN, and
subtraction-overflow cases.

Fixes kuasar-io#244

Signed-off-by: thanhtoantnt <thanhtoantnt@gmail.com>
@thanhtoantnt

Copy link
Copy Markdown
Contributor Author

@novahe Could you review this fix?

thanks! nit: please squash commits

Done.

@thanhtoantnt

Copy link
Copy Markdown
Contributor Author

@novahe could you review this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

checked_compute_delta Integer Overflow

3 participants