5 ms·
The article does not provide any valid argument against "Myth 1. Rust's arithmetic is no safer than C++'s". The information provided in the article shows beyon
by adrian_b 5y ago
The article does not provide any valid argument against "Myth 1. Rust's arithmetic is no safer than C++'s".
The information provided in the article shows beyond any reasonable doubt that both C++ and Rust are exactly equally safe regarding integer overflow.
Rust checks for overflow in debug builds, but it does not check for overflow in release builds.
The same happens in C++. All decent C++ compilers have options for overflow checking, but most developers do not use these options in release builds, for fear that it would affect the performance.
Rust is a little better because it enforces the overflow checking option on debug builds, but C++ is better because the developer may choose to keep the option also in release builds.
So this myth is not busted, it is confirmed.
- oconnor663 5y agoThe generate the same assembly in this particular context, but changes to surrounding code could change that. The fact that signed int overflow is UB in C++ is a major difference. > C++ is better because the developer may choose to keep the option also in release builds You can enable overflow checks in release mode if you like: https://doc.rust-lang.org/cargo/reference/profiles.html https://doc.rust-lang.org/cargo/reference/profiles.html
- adrian_b 5y agoThe fact that integer overflow is undefined in C++ is ugly, but this leaves the solution of this problem to the compiler. The final behavior is decided by the programmer, by using or not using "-fsanitize=signed-integer-overflow" or equivalent options for the compilation. So after the programmer makes a decision, the behavior in C++ is defined and it is either like in debug Rust or like in release Rust, depending on the programmer's decision. While I hate undefined behaviors in C/C++, I prefer to be able to make my own decisions on the behavior of integer overflow, instead of being forced by Rust to ignore overflows in release builds. Edit: Thanks for the Cargo link. If you can change the overflow checking option in the Cargo profile, then there remains absolutely no difference between C++ and Rust regarding integer overflow. The Rust RFC "Integer overflow #560" is totally misleading in this case.
- SAI_Peregrinus 5y agoRust integer overflow is never undefined behavior. It'll either wrap (default release mode) or trap (debug mode, release mode with the flag on). C++ defaults to it being undefined.
- Dylan16807 5y agoIf you give a C++ compiler the opportunity, it will change the assembly into something that gives mathematically impossible results in the name of optimization. It might even generate assembly that crashes on certain values. That's what busts the myth. If you set -fwrapv then they're the same. But that's not standard and not default.
- adrian_b 5y agoThat depends on the compilation options. With the appropriate compilation options the C++ compiler will not make any optimizations that depend on the undefined beahvior for overflow. The only defect here is that the options to do unsafe optimizations are default and you have to give explicitly an option to forbid them, instead of the safe option being default. Since the overflow check is also controlled by a compilation option in Rust and the default for release builds is no checking, there is no difference. Even if Rust might have done less optimizations, an unchecked overflow will still cause a bug.
- Dylan16807 5y ago> The only defect here is that the options to do unsafe optimizations are default and you have to give explicitly an option to forbid them, instead of the safe option being default. And that the option to do the safe thing isn't even part of standard C++ at all! > Since the overflow check is also controlled by a compilation option in Rust and the default for release builds is no checking, there is no difference. Rust has no "undefined behavior" option. It only has overflow and trap. That's also a big difference.
- adrian_b 5y agoThere is no practical difference. Rust either checks for overflow or assumes wrapping, depending on the compilation option "overflow-checks = false/true". The semantics of the 2 behaviors are incompatible so any Rust program written assuming one of the 2 behaviors will fail in certain cases when it is compiled with the contrary option. There is absolutely no difference in the C++ case, because the behavior is also controlled by the compilation options. It does not matter what the language specifications say, when they are overridden by compilation options both for Rust and for C++. The consequence is the same for both languages. You must write the program assuming a desired behavior, but you must also ensure that the program is compiled with the corresponding option, otherwise failures are likely.
- bestouff 5y agoRust also trivially allows to keep overflow checks in release builds.