Is there any way to achieve undefined behavior in Rust without using unsafe
?
Of course, such behavior can be wrapped by a third-party library in a "safe" function so let's assume we're using only the standard one.
Is there any way to achieve undefined behavior in Rust without using unsafe
?
Of course, such behavior can be wrapped by a third-party library in a "safe" function so let's assume we're using only the standard one.
Absolutely, but any such case is a bug with Rust or the standard libary.
My favorite example is LLVM loop optimization can make safe programs crash, which actually occurs due to a poor interaction of Rust and LLVM semantics:
pub fn oops() {
(|| loop {
drop(42)
})()
}
Compiled with optimizations on Rust 1.49.0, this produces the assembly:
playground::oops:
ud2
such behavior can be wrapped by a third-party library in a "safe" function so let's assume we're using only the standard one
The standard library is a "third-party library", so I don't get the distinction.
std
has sometimes contained UB-causing bugs. That particular one was fixed, but you may be able to find other, more current examples by searching for issues tagged I-unsound π₯ on GitHub. β
Beaudette Y
happens to immediately follow X
, something that could happen since the symbols are imported, and i
is zero, Y[0]
would be set to 4 but the function would return 3 (see line 17 of the assembly code). β
Antenatal © 2022 - 2024 β McMap. All rights reserved.