|
|
|
|
|
by unrealhoang
180 days ago
|
|
> logic bugs outside "unsafe" can cause bugs unsafe. This is the wrong understanding of Rust's unsafety encapsulation. For example, no logic bug outside of `unsafe` can cause undefined behavior of Rust std's `Vec` abstraction, which is using underlying unsafe to build. The point that "because unsafe is used so the entire Rust program is also unsafe" is a real major myth. It's as absurd as saying "because Java runtime using unsafe underlying to build so Java is also unsafe". |
|
Why was the fix to this unsafe memory safety bug [0] only changes to code outside of unsafe Rust blocks?[1][2]
Why does the Rustonomicon[3] say the following?
> This code is 100% Safe Rust but it is also completely unsound. Changing the capacity violates the invariants of Vec (that cap reflects the allocated space in the Vec). This is not something the rest of Vec can guard against. It has to trust the capacity field because there's no way to verify it.
> Because it relies on invariants of a struct field, this unsafe code does more than pollute a whole function: it pollutes a whole module. Generally, the only bullet-proof way to limit the scope of unsafe code is at the module boundary with privacy.
[0] https://social.kernel.org/notice/B1JLrtkxEBazCPQHDM
[1] https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux...
[2] https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux...
[3] https://doc.rust-lang.org/nomicon/working-with-unsafe.html