Skip to content

Commit 6e9d5a6

Browse files
committed
rollup merge of #18366 : aochagavia/ascii
2 parents c5cc27f + b8c4eb3 commit 6e9d5a6

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/libstd/ascii.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ impl OwnedAsciiCast for String {
247247

248248
#[inline]
249249
unsafe fn into_ascii_nocheck(self) -> Vec<Ascii> {
250-
let v: Vec<u8> = mem::transmute(self);
251-
v.into_ascii_nocheck()
250+
self.into_bytes().into_ascii_nocheck()
252251
}
253252
}
254253

@@ -260,7 +259,14 @@ impl OwnedAsciiCast for Vec<u8> {
260259

261260
#[inline]
262261
unsafe fn into_ascii_nocheck(self) -> Vec<Ascii> {
263-
mem::transmute(self)
262+
let v = Vec::from_raw_parts(self.len(),
263+
self.capacity(),
264+
mem::transmute(self.as_ptr()));
265+
266+
// We forget `self` to avoid freeing it at the end of the scope
267+
// Otherwise, the returned `Vec` would point to freed memory
268+
mem::forget(self);
269+
v
264270
}
265271
}
266272

@@ -338,7 +344,16 @@ pub trait IntoBytes {
338344

339345
impl IntoBytes for Vec<Ascii> {
340346
fn into_bytes(self) -> Vec<u8> {
341-
unsafe { mem::transmute(self) }
347+
unsafe {
348+
let v = Vec::from_raw_parts(self.len(),
349+
self.capacity(),
350+
mem::transmute(self.as_ptr()));
351+
352+
// We forget `self` to avoid freeing it at the end of the scope
353+
// Otherwise, the returned `Vec` would point to freed memory
354+
mem::forget(self);
355+
v
356+
}
342357
}
343358
}
344359

0 commit comments

Comments
 (0)