Skip to content

Commit 57f971b

Browse files
authored
Auto merge of #36365 - matthew-piziak:silent-overflow, r=eddyb
fix silent overflows on `Step` impls Part of #36110 r? @eddyb
2 parents ec0d1ce + 8f19d5c commit 57f971b

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

src/libcore/iter/range.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ macro_rules! step_impl_unsigned {
9696

9797
#[inline]
9898
fn add_one(&self) -> Self {
99-
*self + 1
99+
Add::add(*self, 1)
100100
}
101101

102102
#[inline]
103103
fn sub_one(&self) -> Self {
104-
*self - 1
104+
Sub::sub(*self, 1)
105105
}
106106

107107
#[inline]
@@ -167,12 +167,12 @@ macro_rules! step_impl_signed {
167167

168168
#[inline]
169169
fn add_one(&self) -> Self {
170-
*self + 1
170+
Add::add(*self, 1)
171171
}
172172

173173
#[inline]
174174
fn sub_one(&self) -> Self {
175-
*self - 1
175+
Sub::sub(*self, 1)
176176
}
177177

178178
#[inline]
@@ -216,12 +216,12 @@ macro_rules! step_impl_no_between {
216216

217217
#[inline]
218218
fn add_one(&self) -> Self {
219-
*self + 1
219+
Add::add(*self, 1)
220220
}
221221

222222
#[inline]
223223
fn sub_one(&self) -> Self {
224-
*self - 1
224+
Sub::sub(*self, 1)
225225
}
226226

227227
#[inline]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -C debug_assertions=yes
12+
13+
use std::panic;
14+
15+
fn main() {
16+
let r = panic::catch_unwind(|| {
17+
let mut it = u8::max_value()..;
18+
it.next().unwrap(); // 255
19+
it.next().unwrap();
20+
});
21+
assert!(r.is_err());
22+
23+
let r = panic::catch_unwind(|| {
24+
let mut it = i8::max_value()..;
25+
it.next().unwrap(); // 127
26+
it.next().unwrap();
27+
});
28+
assert!(r.is_err());
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -C debug_assertions=no
12+
13+
fn main() {
14+
let mut it = u8::max_value()..;
15+
assert_eq!(it.next().unwrap(), 255);
16+
assert_eq!(it.next().unwrap(), u8::min_value());
17+
18+
let mut it = i8::max_value()..;
19+
assert_eq!(it.next().unwrap(), 127);
20+
assert_eq!(it.next().unwrap(), i8::min_value());
21+
}

0 commit comments

Comments
 (0)