-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refinements to unsafe code #157
Conversation
#[allow(unreachable_code)] | ||
const DEFAULT_ZALLOC: Option<alloc_func> = '_blk: { | ||
// this `break 'blk'` construction exists to generate just one compile error and not other | ||
// warnings when multiple allocators are configured. | ||
|
||
#[cfg(feature = "c-allocator")] | ||
break '_blk Some(zlib_rs::allocate::Allocator::C.zalloc); | ||
|
||
#[cfg(feature = "rust-allocator")] | ||
break '_blk Some(zlib_rs::allocate::Allocator::RUST.zalloc); | ||
|
||
None | ||
}; | ||
|
||
#[allow(unreachable_code)] | ||
const DEFAULT_ZFREE: Option<free_func> = '_blk: { | ||
#[cfg(feature = "c-allocator")] | ||
break '_blk Some(zlib_rs::allocate::Allocator::C.zfree); | ||
|
||
#[cfg(feature = "rust-allocator")] | ||
break '_blk Some(zlib_rs::allocate::Allocator::RUST.zfree); | ||
|
||
None | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is handled by zlib-rs. Only one allocator is available there because of the compile_error on line 66
/// * Either | ||
/// - `destLen` is `NULL` | ||
/// - `destLen` satisfies the requirements of `&mut *destLen` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stock zlib happily dereferences a null pointer here. We check for it being null and return an error, which, because zlib has undefined behavior in this case, is compatible.
fn initialize_header_null() -> MaybeUninit<gz_header> { | ||
let mut head = MaybeUninit::<gz_header>::uninit(); | ||
let mut head = MaybeUninit::<gz_header>::zeroed(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test uses some low-level operations still, but we now assume that the memory is initialized, so we have to use zeroed
here.
46f3756
to
c8e4d43
Compare
among other things
Init
andGetHeader
functions now assume that their arguments are properly initialized in the rust sense. Providing uninitialized memory to these functions is UB, though unlikely to actually cause issues across an FFI boundary