Skip to content

Commit 5bcd0a0

Browse files
committed
prevent the creation of integers too big for the target architecture
1 parent 61ab2ea commit 5bcd0a0

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/librustc/middle/trans/adt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ fn ensure_struct_fits_in_address_space(ccx: &CrateContext,
478478
offset += machine::llsize_of_alloc(ccx, llty);
479479

480480
// We can get away with checking for overflow once per iteration,
481-
// because field sizes are less than 1<<60.
481+
// because field sizes are less than 1<<61.
482482
if offset >= ccx.max_obj_size() {
483483
ccx.report_overbig_object(scapegoat);
484484
}
@@ -498,7 +498,7 @@ fn ensure_enum_fits_in_address_space(ccx: &CrateContext,
498498
let discr_size = machine::llsize_of_alloc(ccx, ll_inttype(ccx, discr));
499499
let (field_size, field_align) = union_size_and_align(fields);
500500

501-
// This can't overflow because field_size, discr_size, field_align < 1<<60
501+
// This can't overflow because field_size, discr_size, field_align < 1<<61
502502
let total_size = roundup(discr_size, field_align) + field_size;
503503

504504
if total_size >= ccx.max_obj_size() {

src/librustc/middle/trans/common.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -596,17 +596,34 @@ pub fn C_u64(ccx: &CrateContext, i: u64) -> ValueRef {
596596
}
597597

598598
pub fn C_int<I: AsI64>(ccx: &CrateContext, i: I) -> ValueRef {
599-
C_integral(ccx.int_type(), i.as_i64() as u64, true)
599+
let v = i.as_i64();
600+
601+
match machine::llbitsize_of_real(ccx.int_type()) {
602+
32 => assert!(v < (1<<31) && v >= -(1<<31)),
603+
64 => {},
604+
n => fail!("unsupported target size: {}", n)
605+
}
606+
607+
C_integral(ccx.int_type(), v as u64, true)
600608
}
601609

602610
pub fn C_uint<I: AsU64>(ccx: &CrateContext, i: I) -> ValueRef {
603-
C_integral(ccx.int_type(), i.as_u64(), false)
611+
let v = i.as_u64();
612+
613+
match machine::llbitsize_of_real(ccx.int_type()) {
614+
32 => assert!(v < (1<<32)),
615+
64 => {},
616+
n => fail!("unsupported target size: {}", n)
617+
}
618+
619+
C_integral(ccx.int_type(), v, false)
604620
}
605621

606622
pub trait AsI64 { fn as_i64(self) -> i64; }
607623
pub trait AsU64 { fn as_u64(self) -> u64; }
608624

609-
// FIXME: remove the intptr conversions
625+
// FIXME: remove the intptr conversions, because they
626+
// are host-architecture-dependent
610627
impl AsI64 for i64 { fn as_i64(self) -> i64 { self as i64 }}
611628
impl AsI64 for i32 { fn as_i64(self) -> i64 { self as i64 }}
612629
impl AsI64 for int { fn as_i64(self) -> i64 { self as i64 }}

0 commit comments

Comments
 (0)