servo / rust-smallvec

"Small vector" optimization for Rust: store up to a small number of items on the stack
Apache License 2.0
1.32k stars 141 forks source link

Added test for servo#353 and fixed use after free bug that causes it #354

Closed josephg closed 4 months ago

josephg commented 4 months ago

I took a look at what was causing #353.

Turns out the push_heap function was placing the heap pointer into a local variable before calling self.reserve(). reserve in turn calls realloc() - which can free the old heap allocation & create a new one. When this happens, the old ptr is becomes invalid.

The code was writing to this invalid pointer (ptr.as_ptr().add(len).write(value)) - which is a use-after-free bug.

This PR fixes the bug and adds a regression test.