Skip to content
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

Merged
merged 16 commits into from
Aug 23, 2024
Merged

refinements to unsafe code #157

merged 16 commits into from
Aug 23, 2024

Conversation

folkertdev
Copy link
Collaborator

among other things

  • the Init and GetHeader 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

Comment on lines -59 to -82
#[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
};
Copy link
Collaborator Author

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

Comment on lines +229 to +231
/// * Either
/// - `destLen` is `NULL`
/// - `destLen` satisfies the requirements of `&mut *destLen`
Copy link
Collaborator Author

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.

Comment on lines 411 to 413
fn initialize_header_null() -> MaybeUninit<gz_header> {
let mut head = MaybeUninit::<gz_header>::uninit();
let mut head = MaybeUninit::<gz_header>::zeroed();

Copy link
Collaborator Author

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.

@folkertdev folkertdev merged commit 4079a21 into main Aug 23, 2024
17 checks passed
@folkertdev folkertdev deleted the assume-initialized-memory branch August 23, 2024 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant