On 2014-05-31, at 22:13, Matthieu Monrocq <[email protected]> wrote:
> Another example of memory issue:
>
> foo(left: &Option<Box<str>>, right: &mut Option<Box<str>>) {
> let ptr: &str = *left.unwrap();
>
> right = None;
>
> match ptr.len() { // Watch out! if left and right alias, then ptr is
> no a dangling reference!
> // ...
> }
> }
But the reason why there could be a dangling reference there is the (assumed)
deallocation of Box<str> caused by the assignment to `right`. The type system
should prevent assignment to a variable of type Option<Box<str>> if multiple
mutable references could point to that variable.
> The issue can actually occur in other ways: replace Box<str> by enum Point {
> Integral(int, int), Floating(f64, f64) } and you could manage to write
> integral into floats or vice-versa, which is memory-corruption, not
> segmentation fault.
I don't think that counts as memory-corruption (according to
http://en.wikipedia.org/wiki/Memory_corruption). That may be a bug in program
logic, but then again, there might be a valid use for having two different
pointers interpret the same of partly overlapping memory region as different
types and mutating and using their memory according to their interpretation of
it.
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev