4 ms·
You can still have use-after-free errors when you use array indices. This can happen if you implement a way to "free" elements stored in the vector. "free" shou
by screcth 2y ago
You can still have use-after-free errors when you use array indices.
This can happen if you implement a way to "free" elements stored in the vector.
"free" should be interpreted in a wide sense.
There's no way for Rust to prevent you from marking an array index as free and later using it.
- oconnor663 2y ago> There's no way for Rust to prevent you from marking an array index as free and later using it. I 2/3rds disagree with this. There are three different cases: - Plain Vec<T>. In this case you just can't remove elements. (At least not without screwing up the indexes of other elements, so not in the cases we're talking about here.) - Vec<Option<T>>. In this case you can make index reuse mistakes. However, this is less efficient and less convenient than... - SlotMap<T> or similar. This uses generational indexes to solve the reuse problem, and it provides other nice conveniences. The only real downside is that you need to know about it and take a dependency.
- dmkolobov 2y agoThe consequences of use-after-free are different for the two. In rust it is a logic error, which leads to data corruption or program panics within your application. In C it leads to data corruption and is an attack vector for the entire machine. And yes, while Rust itself doesn’t help you with this type of error, there are plenty of Rust libraries which do.