diff options
| author | Irfan Hudda <irfanhudda@gmail.com> | 2017-04-19 23:59:43 +0530 |
|---|---|---|
| committer | Irfan Hudda <irfanhudda@gmail.com> | 2017-04-19 23:59:43 +0530 |
| commit | 04e9c20228c38c20c5d4f84328332b2c5c98d5a2 (patch) | |
| tree | 12da51879b87420108bd76835eccd89d92be1d60 | |
| parent | f7c641b8b62a36ce23bd000644f19cf8761007b0 (diff) | |
| download | rust-04e9c20228c38c20c5d4f84328332b2c5c98d5a2.tar.gz rust-04e9c20228c38c20c5d4f84328332b2c5c98d5a2.zip | |
next_power_of_two panic on overflow
| -rw-r--r-- | src/libcore/num/mod.rs | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 487b0dba3c4..15ffe9db8ce 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -2345,7 +2345,11 @@ macro_rules! uint_impl { pub fn next_power_of_two(self) -> Self { let bits = size_of::<Self>() * 8; let one: Self = 1; - one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits) + if self == 0 { + 1 + } else { + one << (bits - self.wrapping_sub(one).leading_zeros() as usize) + } } /// Returns the smallest power of two greater than or equal to `n`. If @@ -2363,7 +2367,9 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn checked_next_power_of_two(self) -> Option<Self> { - let npot = self.next_power_of_two(); + let bits = size_of::<Self>() * 8; + let one: Self = 1; + let npot = one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits); if npot >= self { Some(npot) } else { |
