Closed
Description
https://github.com/rust-lang/rust/blob/master/src/liballoc_system/lib.rs#L231-L241
When reallocating, it calculates the new aligned pointer which may have a different offset than the old allocation, which means the pointer might not point to the same point in the allocation anymore, which is bad. Should probably be changed to just do a completely new allocation and manually copy over the data.
Note that as long as there is no stable way to specify an alignment higher than 8 or 16 bytes (for x86 and x86_64 respectively) this bug cannot be encountered in stable Rust, however it is still important to fix.
Example that reproduces the issue:
#![feature(attr_literals, repr_align)]
#[repr(align(256))]
struct Foo(usize);
fn main() {
let mut foo = vec![Foo(273)];
for i in 0..0x1000 {
foo.reserve_exact(i);
assert!(foo[0].0 == 273);
assert!(foo.as_ptr() as usize & 0xff == 0);
foo.shrink_to_fit();
assert!(foo[0].0 == 273);
assert!(foo.as_ptr() as usize & 0xff == 0);
}
}