-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
miri: checked_pow: overflowing shift by 64 in unchecked_shl
#120537
Comments
@NCGThompson @oli-obk I feel like int macro changes would have best been split out into a separate PR^^ |
WG-prioritization assigning priority (Zulip discussion). @rustbot label -I-prioritize +P-high +T-compiler |
// SAFETY: We just checked this is a power of two. and above zero.
let power_used = unsafe { intrinsics::cttz_nonzero(self) as u32 };
if exp > Self::BITS / power_used { return None; } // Division of constants is free
@rustbot claim |
Just to make sure it's not lost -- we also need to handle the case where |
We want to check if
However, we can't use exact division, only floor division (f.d.). Unfortunately,
and
Since we are dealing exclusively with non-negative integers, we can say that
, that
, and that
From there, we can work out that
and if exp > (Self::BITS - 1) / power_used { return None; } @oli-obk Now you don't need to go math golfing. |
For signed types, note that |
Right. Since overflow on |
Oh, you mean this? Then OK, but that could use a comment. rust/library/core/src/num/int_macros.rs Lines 1402 to 1404 in 11f32b7
|
Rollup merge of rust-lang#120562 - oli-obk:revert_stuff, r=cuviper Revert unsound libcore changes fixes rust-lang#120537 these were introduced in rust-lang#119911
The revert landed after the branch move, so let's re-open until that's backported. |
Backport was done in #121069. |
Code
I tried this code:
I expected that a
checked_*
operation would specifically not cause UB on overflow.Instead, this happened (playground):
Version it worked on
It most recently worked on:
cargo 1.77.0-nightly (84976cd69 2024-01-12)
Version with regression
cargo 1.77.0-nightly (7bb7b5395 2024-01-20)
I didn't bisect this, but I believe it started happening with GH PR #119911.
Backtrace
Backtrace
I believe GH PR #119911 is the cause.
The text was updated successfully, but these errors were encountered: