]> git.lizzy.rs Git - rust.git/commitdiff
Fix compile-time integer overflow when using ! on unsigned values
authorinrustwetrust <inrustwetrust@users.noreply.github.com>
Sun, 17 May 2015 08:04:15 +0000 (10:04 +0200)
committerinrustwetrust <inrustwetrust@users.noreply.github.com>
Sun, 17 May 2015 08:04:15 +0000 (10:04 +0200)
Since the values are stored in a u64 internally, we need to be mask away the
high bits after applying the ! operator. Otherwise, these bits will be set to
one, causing overflow.

src/librustc/middle/const_eval.rs
src/test/run-pass/issue-23968-const-not-overflow.rs [new file with mode: 0644]

index 091092e3b607992adc637a8fd0ffc0ac80e8c484..72415f543368e6f1294eb7d970b49482009c1a0e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -34,7 +34,7 @@
 use std::num::wrapping::OverflowingOps;
 use std::cmp::Ordering;
 use std::collections::hash_map::Entry::Vacant;
-use std::{i8, i16, i32, i64};
+use std::{i8, i16, i32, i64, u8, u16, u32, u64};
 use std::rc::Rc;
 
 fn lookup_const<'a>(tcx: &'a ty::ctxt, e: &Expr) -> Option<&'a Expr> {
@@ -461,6 +461,16 @@ pub fn const_uint_checked_neg<'a>(
     Ok(const_uint((!a).wrapping_add(1)))
 }
 
+fn const_uint_not(a: u64, opt_ety: Option<UintTy>) -> const_val {
+    let mask = match opt_ety {
+        Some(UintTy::U8) => u8::MAX as u64,
+        Some(UintTy::U16) => u16::MAX as u64,
+        Some(UintTy::U32) => u32::MAX as u64,
+        None | Some(UintTy::U64) => u64::MAX,
+    };
+    const_uint(!a & mask)
+}
+
 macro_rules! overflow_checking_body {
     ($a:ident, $b:ident, $ety:ident, $overflowing_op:ident,
      lhs: $to_8_lhs:ident $to_16_lhs:ident $to_32_lhs:ident,
@@ -677,7 +687,7 @@ fn fromb(b: bool) -> const_val { const_int(b as i64) }
       ast::ExprUnary(ast::UnNot, ref inner) => {
         match try!(eval_const_expr_partial(tcx, &**inner, ety)) {
           const_int(i) => const_int(!i),
-          const_uint(i) => const_uint(!i),
+          const_uint(i) => const_uint_not(i, expr_uint_type),
           const_bool(b) => const_bool(!b),
           const_str(_) => signal!(e, NotOnString),
           const_float(_) => signal!(e, NotOnFloat),
diff --git a/src/test/run-pass/issue-23968-const-not-overflow.rs b/src/test/run-pass/issue-23968-const-not-overflow.rs
new file mode 100644 (file)
index 0000000..95f6591
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+const U8_MAX_HALF: u8 = !0u8 / 2;
+const U16_MAX_HALF: u16 = !0u16 / 2;
+const U32_MAX_HALF: u32 = !0u32 / 2;
+const U64_MAX_HALF: u64 = !0u64 / 2;
+
+fn main() {
+    assert_eq!(U8_MAX_HALF, 0x7f);
+    assert_eq!(U16_MAX_HALF, 0x7fff);
+    assert_eq!(U32_MAX_HALF, 0x7fff_ffff);
+    assert_eq!(U64_MAX_HALF, 0x7fff_ffff_ffff_ffff);
+}