@@ -260,7 +260,8 @@ pub unsafe fn decompress_c_with_capacity(
260260 opaque : std:: ptr:: null_mut :: < libc:: c_void > ( ) ,
261261 } ;
262262
263- let mut dest = vec ! [ 0u8 ; capacity] ;
263+ // Deliberately use uninitialized memory for the output.
264+ let mut dest = Vec :: < u8 > :: with_capacity ( capacity) ;
264265
265266 strm. bzalloc = None ;
266267 strm. bzfree = None ;
@@ -271,7 +272,7 @@ pub unsafe fn decompress_c_with_capacity(
271272 return ( ret, vec ! [ ] ) ;
272273 }
273274 strm. avail_in = source_len;
274- strm. avail_out = dest. len ( ) as _ ;
275+ strm. avail_out = dest. capacity ( ) as _ ;
275276 strm. next_in = source as * mut libc:: c_char ;
276277 strm. next_out = dest. as_mut_ptr ( ) . cast :: < core:: ffi:: c_char > ( ) ;
277278
@@ -282,10 +283,14 @@ pub unsafe fn decompress_c_with_capacity(
282283 BZ2_bzDecompressEnd ( & mut strm) ;
283284 break BZ_UNEXPECTED_EOF ;
284285 } else {
285- let used = dest. len ( ) - strm. avail_out as usize ;
286+ let used = dest. capacity ( ) - strm. avail_out as usize ;
287+
288+ // We've written this many (initialized!) bytes to the output.
289+ dest. set_len ( used) ;
290+
286291 // The dest buffer is full.
287- let add_space: u32 = Ord :: max ( 1024 , dest. len ( ) . try_into ( ) . unwrap ( ) ) ;
288- dest. resize ( dest . len ( ) + add_space as usize , 0 ) ;
292+ let add_space: u32 = Ord :: max ( 1024 , dest. capacity ( ) . try_into ( ) . unwrap ( ) ) ;
293+ dest. reserve ( add_space as usize ) ;
289294
290295 // If resize() reallocates, it may have moved in memory.
291296 strm. next_out = dest. as_mut_ptr ( ) . cast :: < i8 > ( ) . wrapping_add ( used) ;
@@ -305,11 +310,8 @@ pub unsafe fn decompress_c_with_capacity(
305310 }
306311 } ;
307312
308- dest. truncate (
309- ( ( u64:: from ( strm. total_out_hi32 ) << 32 ) + u64:: from ( strm. total_out_lo32 ) )
310- . try_into ( )
311- . unwrap ( ) ,
312- ) ;
313+ let total = ( u64:: from ( strm. total_out_hi32 ) << 32 ) + u64:: from ( strm. total_out_lo32 ) ;
314+ dest. set_len ( usize:: try_from ( total) . unwrap ( ) ) ;
313315
314316 ( ret, dest)
315317 }
@@ -341,7 +343,8 @@ pub unsafe fn decompress_rs_with_capacity(
341343 opaque : std:: ptr:: null_mut :: < libc:: c_void > ( ) ,
342344 } ;
343345
344- let mut dest = vec ! [ 0u8 ; capacity] ;
346+ // Deliberately use uninitialized memory for the output.
347+ let mut dest = Vec :: < u8 > :: with_capacity ( capacity) ;
345348
346349 strm. bzalloc = None ;
347350 strm. bzfree = None ;
@@ -352,7 +355,7 @@ pub unsafe fn decompress_rs_with_capacity(
352355 return ( ret, vec ! [ ] ) ;
353356 }
354357 strm. avail_in = source_len;
355- strm. avail_out = dest. len ( ) as _ ;
358+ strm. avail_out = dest. capacity ( ) as _ ;
356359 strm. next_in = source as * mut libc:: c_char ;
357360 strm. next_out = dest. as_mut_ptr ( ) . cast :: < core:: ffi:: c_char > ( ) ;
358361
@@ -363,10 +366,14 @@ pub unsafe fn decompress_rs_with_capacity(
363366 BZ2_bzDecompressEnd ( & mut strm) ;
364367 break BZ_UNEXPECTED_EOF ;
365368 } else {
366- let used = dest. len ( ) - strm. avail_out as usize ;
369+ let used = dest. capacity ( ) - strm. avail_out as usize ;
370+
371+ // We've written this many (initialized!) bytes to the output.
372+ dest. set_len ( used) ;
373+
367374 // The dest buffer is full.
368- let add_space: u32 = Ord :: max ( 1024 , dest. len ( ) . try_into ( ) . unwrap ( ) ) ;
369- dest. resize ( dest . len ( ) + add_space as usize , 0 ) ;
375+ let add_space: u32 = Ord :: max ( 1024 , dest. capacity ( ) . try_into ( ) . unwrap ( ) ) ;
376+ dest. reserve ( add_space as usize ) ;
370377
371378 // If resize() reallocates, it may have moved in memory.
372379 strm. next_out = dest. as_mut_ptr ( ) . cast :: < i8 > ( ) . wrapping_add ( used) ;
@@ -386,11 +393,8 @@ pub unsafe fn decompress_rs_with_capacity(
386393 }
387394 } ;
388395
389- dest. truncate (
390- ( ( u64:: from ( strm. total_out_hi32 ) << 32 ) + u64:: from ( strm. total_out_lo32 ) )
391- . try_into ( )
392- . unwrap ( ) ,
393- ) ;
396+ let total = ( u64:: from ( strm. total_out_hi32 ) << 32 ) + u64:: from ( strm. total_out_lo32 ) ;
397+ dest. set_len ( usize:: try_from ( total) . unwrap ( ) ) ;
394398
395399 ( ret, dest)
396400 }
0 commit comments