From 3f980785fb55e31aca9215faa474d751e5a28d69 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 27 Oct 2019 17:31:01 +0000 Subject: [PATCH] doc: explain why it is unsafe to construct Vec from Vec Co-authored-by: Steve Klabnik --- src/liballoc/vec.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 641f9eafa8d23..5e733fa43d705 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -411,7 +411,11 @@ impl Vec { /// /// Violating these may cause problems like corrupting the allocator's /// internal data structures. For example it is **not** safe - /// to build a `Vec` from a pointer to a C `char` array and a `size_t`. + /// to build a `Vec` from a pointer to a C `char` array with length `size_t`. + /// It's also not safe to build one from a `Vec` and its length, because + /// the allocator cares about the alignment, and these two types have different + /// alignments. The buffer was allocated with alignment 2 (for `u16`), but after + /// turning it into a `Vec` it'll be deallocated with alignment 1. /// /// The ownership of `ptr` is effectively transferred to the /// `Vec` which may then deallocate, reallocate or change the