Skip to content

Commit

Permalink
Fixed housleyjk#330: rust UB runtime panic
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDan64 committed Mar 30, 2022
1 parent f6db384 commit 2bf3737
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/deflate/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem;
use std::mem::{self, MaybeUninit};
use std::slice;

use super::ffi;
Expand Down Expand Up @@ -67,7 +67,7 @@ impl Compressor {
debug_assert!(window_bits <= 15, "Received too large window size.");

unsafe {
let mut stream: Box<ffi::z_stream> = Box::new(mem::zeroed());
let mut stream = MaybeUninit::<ffi::z_stream>::zeroed();
let result = ffi::deflateInit2_(
stream.as_mut(),
9,
Expand All @@ -79,7 +79,9 @@ impl Compressor {
mem::size_of::<ffi::z_stream>() as c_int,
);
assert!(result == ffi::Z_OK, "Failed to initialize compresser.");
Compressor { stream: stream }
Compressor {
stream: Box::new(stream.assume_init()),
}
}
}

Expand Down Expand Up @@ -139,15 +141,17 @@ impl Decompressor {
debug_assert!(window_bits <= 15, "Received too large window size.");

unsafe {
let mut stream: Box<ffi::z_stream> = Box::new(mem::zeroed());
let mut stream = MaybeUninit::<ffi::z_stream>::zeroed();
let result = ffi::inflateInit2_(
stream.as_mut(),
-window_bits as c_int,
ZLIB_VERSION.as_ptr() as *const c_char,
mem::size_of::<ffi::z_stream>() as c_int,
);
assert!(result == ffi::Z_OK, "Failed to initialize decompresser.");
Decompressor { stream: stream }
Decompressor {
stream: Box::new(stream.assume_init()),
}
}
}

Expand Down

0 comments on commit 2bf3737

Please sign in to comment.