From 2bf37378c6fb91de51c1fb777cdacd398ae45359 Mon Sep 17 00:00:00 2001 From: Daniel Kolsoi Date: Wed, 30 Mar 2022 10:51:00 -0400 Subject: [PATCH] Fixed #330: rust UB runtime panic --- src/deflate/context.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/deflate/context.rs b/src/deflate/context.rs index 2fa2e23..b5e3073 100644 --- a/src/deflate/context.rs +++ b/src/deflate/context.rs @@ -1,4 +1,4 @@ -use std::mem; +use std::mem::{self, MaybeUninit}; use std::slice; use super::ffi; @@ -67,7 +67,7 @@ impl Compressor { debug_assert!(window_bits <= 15, "Received too large window size."); unsafe { - let mut stream: Box = Box::new(mem::zeroed()); + let mut stream = MaybeUninit::::zeroed(); let result = ffi::deflateInit2_( stream.as_mut(), 9, @@ -79,7 +79,9 @@ impl Compressor { mem::size_of::() as c_int, ); assert!(result == ffi::Z_OK, "Failed to initialize compresser."); - Compressor { stream: stream } + Compressor { + stream: Box::new(stream.assume_init()), + } } } @@ -139,7 +141,7 @@ impl Decompressor { debug_assert!(window_bits <= 15, "Received too large window size."); unsafe { - let mut stream: Box = Box::new(mem::zeroed()); + let mut stream = MaybeUninit::::zeroed(); let result = ffi::inflateInit2_( stream.as_mut(), -window_bits as c_int, @@ -147,7 +149,9 @@ impl Decompressor { mem::size_of::() as c_int, ); assert!(result == ffi::Z_OK, "Failed to initialize decompresser."); - Decompressor { stream: stream } + Decompressor { + stream: Box::new(stream.assume_init()), + } } }