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

gzip: make header functions unsafe and document preconditions #235

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libz-rs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,10 @@ pub unsafe extern "C-unwind" fn deflate(strm: *mut z_stream, flush: i32) -> c_in
/// - `strm` satisfies the requirements of `&mut *strm` and was initialized with [`deflateInit_`] or similar
/// * Either
/// - `head` is `NULL`
/// - `head` satisfies the requirements of `&mut *head`
/// - `head` satisfies the requirements of `&mut *head` and satisfies the following:
/// - `head.extra` is `NULL` or is readable for at least `head.extra_len` bytes
/// - `head.name` is `NULL` or satisfies the requirements of [`core::ffi::CStr::from_ptr`]
/// - `head.comment` is `NULL` or satisfies the requirements of [`core::ffi::CStr::from_ptr`]
#[export_name = prefix!(deflateSetHeader)]
pub unsafe extern "C-unwind" fn deflateSetHeader(strm: *mut z_stream, head: gz_headerp) -> c_int {
let Some(stream) = (unsafe { DeflateStream::from_stream_mut(strm) }) else {
Expand Down
16 changes: 12 additions & 4 deletions zlib-rs/src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2968,7 +2968,15 @@ impl Heap {
}
}

pub fn set_header<'a>(
/// # Safety
///
/// The caller must guarantee:
///
/// * If `head` is `Some`
/// - `head.extra` is `NULL` or is readable for at least `head.extra_len` bytes
/// - `head.name` is `NULL` or satisfies the requirements of [`core::ffi::CStr::from_ptr`]
/// - `head.comment` is `NULL` or satisfies the requirements of [`core::ffi::CStr::from_ptr`]
pub unsafe fn set_header<'a>(
stream: &mut DeflateStream<'a>,
head: Option<&'a mut gz_header>,
) -> ReturnCode {
Expand Down Expand Up @@ -3675,7 +3683,7 @@ mod test {
unreachable!()
};

set_header(stream, Some(&mut header));
unsafe { set_header(stream, Some(&mut header)) };

let input = b"Hello World\n";
stream.next_in = input.as_ptr() as *mut _;
Expand Down Expand Up @@ -3743,7 +3751,7 @@ mod test {
unreachable!()
};

set_header(stream, Some(&mut header));
unsafe { set_header(stream, Some(&mut header)) };

let input = b"Hello World\n";
stream.next_in = input.as_ptr() as *mut _;
Expand Down Expand Up @@ -3804,7 +3812,7 @@ mod test {
};

assert_eq!(
crate::inflate::get_header(stream, Some(&mut header)),
unsafe { crate::inflate::get_header(stream, Some(&mut header)) },
ReturnCode::Ok
);

Expand Down
10 changes: 9 additions & 1 deletion zlib-rs/src/inflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,15 @@ pub fn end<'a>(stream: &'a mut InflateStream<'a>) -> &'a mut z_stream {
stream
}

pub fn get_header<'a>(
/// # Safety
///
/// The caller must guarantee:
///
/// * If `head` is `Some`:
// - If `head.extra` is not NULL, it must be writable for at least `head.extra_max` bytes
/// - if `head.name` is not NULL, it must be writable for at least `head.name_max` bytes
/// - if `head.comment` is not NULL, it must be writable for at least `head.comm_max` bytes
pub unsafe fn get_header<'a>(
stream: &mut InflateStream<'a>,
head: Option<&'a mut gz_header>,
) -> ReturnCode {
Expand Down
Loading