Question:
Initially I wrote a Heap implementation in Rust, but I was getting strange segfaults, so I narrowed down the code to this example, which reproduces the behavior.The result I get from this is:
c
, there is no segfault and the correct value is printed for the heap root.c
can’t affect heap
, but it does. What am I missing?Answer:
You’ve got a use-after-free. Inpush()
, you assign a temporary to self.root
. The temporary’s lifetime is finished of the statement and you’re pointing to freed memory. Any further use will cause undefined behavior.Miri reports it (Tools->Miri in the playground):
If you have better answer, please add a comment about this, thank you!