Skip to content

Commit faf3bcd

Browse files
committed
Accommodate simple cases of arith-overflow in rustc related crates.
1 parent f0404c3 commit faf3bcd

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

src/librustc_trans/trans/adt.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,9 @@ fn load_discr(bcx: Block, ity: IntType, ptr: ValueRef, min: Disr, max: Disr)
778778
assert!(bits <= 64);
779779
let bits = bits as uint;
780780
let mask = (-1u64 >> (64 - bits)) as Disr;
781-
if (max + 1) & mask == min & mask {
781+
// For a (max) discr of -1, max will be `-1 as usize`, which overflows.
782+
// However, that is fine here (it would still represent the full range),
783+
if (max.wrapping_add(1)) & mask == min & mask {
782784
// i.e., if the range is everything. The lo==hi case would be
783785
// rejected by the LLVM verifier (it would mean either an
784786
// empty set, which is impossible, or the entire range of the
@@ -787,7 +789,7 @@ fn load_discr(bcx: Block, ity: IntType, ptr: ValueRef, min: Disr, max: Disr)
787789
} else {
788790
// llvm::ConstantRange can deal with ranges that wrap around,
789791
// so an overflow on (max + 1) is fine.
790-
LoadRangeAssert(bcx, ptr, min, (max+1), /* signed: */ True)
792+
LoadRangeAssert(bcx, ptr, min, (max.wrapping_add(1)), /* signed: */ True)
791793
}
792794
}
793795

src/librustc_typeck/astconv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ pub fn opt_ast_region_to_region<'tcx>(
205205

206206
if len == 2 && i == 0 {
207207
m.push_str(" or ");
208-
} else if i == len - 2 {
208+
} else if i + 2 == len {
209209
m.push_str(", or ");
210-
} else if i != len - 1 {
210+
} else if i + 1 != len {
211211
m.push_str(", ");
212212
}
213213
}

src/libsyntax/ext/deriving/encodable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
181181
Struct(ref fields) => {
182182
let emit_struct_field = cx.ident_of("emit_struct_field");
183183
let mut stmts = Vec::new();
184-
let last = fields.len() - 1;
185184
for (i, &FieldInfo {
186185
name,
187186
ref self_,
@@ -204,6 +203,7 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
204203
lambda));
205204

206205
// last call doesn't need a try!
206+
let last = fields.len() - 1;
207207
let call = if i != last {
208208
cx.expr_try(span, call)
209209
} else {

0 commit comments

Comments
 (0)