3 ms·
> I see that there is one use-after-free bug, and lots of out of range access bugs. How does that happen? The use after free [1] is because of unsafe code, spe
by steveklabnik 7y ago
> I see that there is one use-after-free bug, and lots of out of range access bugs. How does that happen?
The use after free [1] is because of unsafe code, specifically, a function that says "trust me, I know what the type of this is."
I only checked a few of the "out of bounds" bugs, and they all are panics [2], which are not memory unsafe. Of course, someone could cause a real one with the use of unsafe.
1: https://github.com/shepmaster/sxd-document/issues/47#issuecomment-311688608 https://github.com/shepmaster/sxd-document/issues/47#issueco...
2: https://github.com/image-rs/image-tiff/issues/28 https://github.com/image-rs/image-tiff/issues/28
- youdontknowtho 7y agoSomeone else mentioned it in another thread, but the propagation of issues related to code in unsafe regions to other sections of code that are nominally safe is an ongoing research area. It sounds really cool, though. Would love to hear more about it.
- steveklabnik 7y agoI think that's a bit exaggerated. The limits are known: unsafe code can cause problems when combined with any other code in the same module. If you use unsafe, you need to check the whole module, not just the unsafe block. This has been known for a long time, but people tend to not go into detail when talking about this because it's laborious and not really the point.
- johnisgood 7y agoI know that I could probably find this out myself but is there a command-line switch that would make the use of unsafe {} a compile-time error? Of course the alternative is just a quick grep for it, but personally I would feel safer if the code did not compile at all in the presence of a compiler switch/flag and unsafe blocks.
- steveklabnik 7y agoSo, there is, but it only works for your code. You can do it via an attribute in the source (#![forbid(unsafe)]) or via a command line flag (cargo rustc -- -D unsafe). This will not check your dependencies. You can use other tools, like cargo-geiger https://github.com/anderejd/cargo-geiger https://github.com/anderejd/cargo-geiger to check your dependencies. And of course, at the lowest levels, doing anything useful requires unsafe, since your operating system doesn't expose a Rust API to do tasks.
- johnisgood 7y agoThank you! I will use that attribute wherever I can.