diff --git a/src/decode.rs b/src/decode.rs index 527f5e8..67cc58b 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -105,8 +105,7 @@ pub fn decode_engine>( input: T, engine: &E, ) -> Result, DecodeError> { - let mut buffer = Vec::::with_capacity(input.as_ref().len() * 4 / 3); - + let mut buffer = Vec::new(); decode_engine_vec(input, &mut buffer, engine).map(|_| buffer) } @@ -156,11 +155,14 @@ pub fn decode_engine_vec<'e, 'o, E: Engine, T: AsRef<[u8]>>( let starting_output_len = buffer.len(); let estimate = engine.decoded_length_estimate(input_bytes.len()); - let total_len_estimate = estimate - .decoded_length_estimate() + let len_estimate = estimate.decoded_length_estimate(); + let total_len_estimate = len_estimate .checked_add(starting_output_len) .expect("Overflow when calculating output buffer length"); - buffer.resize(total_len_estimate, 0); + buffer.reserve(len_estimate); + unsafe { + buffer.set_len(total_len_estimate); + } let bytes_written; { diff --git a/src/lib.rs b/src/lib.rs index 3ad65b6..bf7f81d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,7 +85,7 @@ variant_size_differences, warnings )] -#![forbid(unsafe_code)] +// #![forbid(unsafe_code)] #![cfg_attr(not(any(feature = "std", test)), no_std)] #[cfg(all(feature = "alloc", not(any(feature = "std", test))))]