Skip to content

Commit 90142a9

Browse files
committed
Add details about integer overflow
1 parent 644a4ff commit 90142a9

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

Diff for: src/behavior-not-considered-unsafe.md

+29-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
## Behavior not considered unsafe
2-
3-
This is a list of behavior not considered *unsafe* in Rust terms, but that may
4-
be undesired.
5-
6-
* Deadlocks
7-
* Leaks of memory and other resources
8-
* Exiting without calling destructors
9-
* Integer overflow
10-
- Overflow is considered "unexpected" behavior and is always user-error,
11-
unless the `wrapping` primitives are used. In non-optimized builds, the compiler
12-
will insert debug checks that panic on overflow, but in optimized builds overflow
13-
instead results in wrapped values. See [RFC 560] for the rationale and more details.
1+
## Behavior not considered `unsafe`
2+
3+
The Rust compiler does not consider the following behaviors _unsafe_,
4+
though a programmer may (should) find them undesirable, unexpected,
5+
or erroneous.
6+
7+
##### Deadlocks
8+
##### Leaks of memory and other resources
9+
##### Exiting without calling destructors
10+
##### Integer overflow
11+
12+
If a program contains arithmetic overflow, the programmer has made an
13+
error.
14+
15+
When the programmer has enabled `debug_assert!` assertions (for
16+
example, by enabling a non-optimized build), the compiler will insert
17+
dynamic checks that `panic` on overflow. Other kinds of builds may
18+
result in silently wrapped values on overflow.
19+
20+
The integral types provide inherent methods to allow programmers
21+
explicitly to perform wrapping arithmetic. For example, (using UFCS)
22+
`i32::wrapping_add` provides two's complement, wrapping addition, as
23+
in `a + b` in the C programming language.
24+
25+
The standard library also provides a `Wrapping<T>` newtype which
26+
overloads arithmetic operators by way of the `WrappingOps` trait.
27+
28+
See [RFC 560] for error conditions, rationale, and more details about
29+
integer overflow.
1430

1531
[RFC 560]: https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md

0 commit comments

Comments
 (0)