Skip to content
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
15 changes: 14 additions & 1 deletion libbz2-rs-sys/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,20 @@ pub(crate) mod c_allocator {
pub(crate) static ALLOCATOR: (AllocFunc, FreeFunc) = (self::allocate, self::deallocate);

unsafe extern "C" fn allocate(_opaque: *mut c_void, count: c_int, size: c_int) -> *mut c_void {
unsafe { libc::malloc((count * size) as usize) }
// NOTE: allocations bigger than isize::MAX are UB in LLVM.
let (Ok(count), Ok(size)) = (isize::try_from(count), isize::try_from(size)) else {
return core::ptr::null_mut();
};

let Some(len) = count.checked_mul(size) else {
return core::ptr::null_mut();
};

let Ok(len) = usize::try_from(len) else {

@bjorn3 bjorn3 May 11, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe convert to usize first to avoid unnecessary overflows? You did have to check for allocations >isize::MAX in that case though. LLVM considers those UB.

return core::ptr::null_mut();
};

unsafe { libc::malloc(len) }
}

unsafe extern "C" fn deallocate(_opaque: *mut c_void, ptr: *mut c_void) {
Expand Down
16 changes: 8 additions & 8 deletions libbz2-rs-sys/src/bzlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ macro_rules! prefix {

pub(crate) use prefix;

/// The version of the zlib library.
/// The version of the libbzip2-rs-sys library.
///
/// Its value is a pointer to a NULL-terminated sequence of bytes.
///
/// The version string for this release is `
#[doc = libbz2_rs_sys_version!()]
/// `:
///
/// - The first component is the version of stock zlib that this release is compatible with
/// - The final component is the zlib-rs version used to build this release.
/// - The first component is the version of stock bzip2 that this release is compatible with
/// - The final component is the libbzip2-rs-sys version used to build this release.
#[cfg_attr(feature = "export-symbols", export_name = prefix!(BZ2_bzlibVersion))]
#[cfg(feature = "stdio")]
pub const extern "C" fn BZ2_bzlibVersion() -> *const core::ffi::c_char {
Expand Down Expand Up @@ -1058,7 +1058,7 @@ impl TryFrom<i32> for Action {
/// - after [`BZ2_bzCompressEnd`]
/// - [`BZ_PARAM_ERROR`] if any of
/// - `strm.is_null()`
/// - `strm.s.is_null()`
/// - `strm.state.is_null()`
/// - action is not one of [`BZ_RUN`], [`BZ_FLUSH`] or [`BZ_FINISH`]
/// - [`BZ_RUN_OK`] successfully compressed, but ran out of input or output space
/// - [`BZ_FLUSH_OK`] not all compressed data has been written to the output yet
Expand Down Expand Up @@ -1173,7 +1173,7 @@ fn compress_loop(strm: &mut BzStream<EState>, s: &mut EState, action: i32) -> Re
///
/// - [`BZ_PARAM_ERROR`] if any of
/// - `strm.is_null()`
/// - `strm.s.is_null()`
/// - `strm.state.is_null()`
/// - no [valid allocator](bz_stream#custom-allocators) could be configured
/// - [`BZ_OK`] otherwise
///
Expand Down Expand Up @@ -1596,7 +1596,7 @@ pub(crate) fn index_into_f(index: u32, cftab: &[u32; 257]) -> u8 {

macro_rules! GET_LL4 {
($s:expr, $i:expr) => {
$s.ll4.as_slice()[($s.tPos >> 1) as usize] as u32 >> ($s.tPos << 2 & 0x4) & 0xf
$s.ll4.as_slice()[($s.tPos >> 1) as usize] as u32 >> ($i << 2 & 0x4) & 0xf
};
}

Expand Down Expand Up @@ -1766,7 +1766,7 @@ fn un_rle_obuf_to_output_small(strm: &mut BzStream<DState>, s: &mut DState) -> b
///
/// - [`BZ_PARAM_ERROR`] if any of
/// - `strm.is_null()`
/// - `strm.s.is_null()`
/// - `strm.state.is_null()`
/// - `strm.avail_out < 1`
/// - [`BZ_DATA_ERROR`] if a data integrity error is detected in the compressed stream
/// - [`BZ_DATA_ERROR_MAGIC`] if the compressed stream doesn't begin with the right magic bytes
Expand Down Expand Up @@ -1879,7 +1879,7 @@ pub(crate) fn BZ2_bzDecompressHelp(strm: &mut BzStream<DState>) -> ReturnCode {
///
/// - [`BZ_PARAM_ERROR`] if any of
/// - `strm.is_null()`
/// - `strm.s.is_null()`
/// - `strm.state.is_null()`
/// - no [valid allocator](bz_stream#custom-allocators) could be configured
/// - [`BZ_OK`] otherwise
///
Expand Down
Loading