File tree Expand file tree Collapse file tree 1 file changed +19
-4
lines changed Expand file tree Collapse file tree 1 file changed +19
-4
lines changed Original file line number Diff line number Diff 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
339345impl 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
You can’t perform that action at this time.
0 commit comments