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

Initial cleanup #3

Merged
merged 6 commits into from
Jan 8, 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
60 changes: 28 additions & 32 deletions .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,38 +76,34 @@ jobs:
files: lcov.info
fail_ci_if_error: false

# obviously we want this in the future, but right now there are a bunch of clippy failures
# clippy:
# name: Clippy
# strategy:
# matrix:
# include:
# - target: x86_64-unknown-linux-gnu
# runs-on: ubuntu-latest
# steps:
# - name: Checkout sources
# uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
# with:
# persist-credentials: false
# - name: Install rust toolchain
# uses: dtolnay/rust-toolchain@be73d7920c329f220ce78e0234b8f96b7ae60248
# with:
# toolchain: stable
# components: clippy
# targets: ${{matrix.target}}
# - name: Rust cache
# uses: Swatinem/rust-cache@3cf7f8cc28d1b4e7d01e3783be10a97d55d483c8
# with:
# shared-key: "stable-${{matrix.target}}"
#
# - name: Run clippy
# run: cargo clippy --target ${{matrix.target}} --workspace --all-targets --all-features -- -D warnings
# - name: Run clippy (fuzzers)
# run: cargo clippy --target ${{matrix.target}} --manifest-path ./fuzz/Cargo.toml --all-targets -- -D warnings
# if: ${{matrix.fuzzer}}
# - name: Run clippy (fuzz_rand_shim)
# run: cargo clippy --target ${{matrix.target}} --manifest-path ./fuzz/fuzz_rand_shim/Cargo.toml --all-targets -- -D warnings
# if: ${{matrix.fuzzer}}
clippy:
name: Clippy
strategy:
matrix:
include:
- target: x86_64-unknown-linux-gnu
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
with:
persist-credentials: false
- name: Install rust toolchain
uses: dtolnay/rust-toolchain@be73d7920c329f220ce78e0234b8f96b7ae60248
with:
toolchain: stable
components: clippy
targets: ${{matrix.target}}
- name: Rust cache
uses: Swatinem/rust-cache@3cf7f8cc28d1b4e7d01e3783be10a97d55d483c8
with:
shared-key: "stable-${{matrix.target}}"

- name: Run clippy
run: cargo clippy --target ${{matrix.target}} --workspace --all-targets --all-features -- -D warnings
- name: Run clippy (fuzzers)
run: cargo clippy --target ${{matrix.target}} --manifest-path ./fuzz/Cargo.toml --all-targets -- -D warnings
if: ${{matrix.fuzzer}}

fuzz:
name: Smoke-test fuzzing targets
Expand Down
4 changes: 2 additions & 2 deletions examples/uncompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {

let mut dest_vec = vec![0u8; 1 << 28];

let mut dest_len = dest_vec.len() as usize;
let mut dest_len = dest_vec.len();
let dest = dest_vec.as_mut_ptr();

let source = input.as_ptr();
Expand All @@ -46,7 +46,7 @@ fn main() {
panic!("error {err}");
}

dest_vec.truncate(dest_len as usize);
dest_vec.truncate(dest_len);

let path = PathBuf::from(path);
std::fs::write(path.with_extension(""), &dest_vec).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/inflate_chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fuzz_target!(|input: (String, usize)| {
let output = String::from_utf8(output).unwrap();

unsafe {
let err = zlib::inflate::inflateEnd(&mut stream);
let err = zlib::inflateEnd(&mut stream);
let return_code: ReturnCode = ReturnCode::from(err);
assert_eq!(ReturnCode::Ok, return_code);
}
Expand Down
4 changes: 2 additions & 2 deletions src/adler32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn adler32_rust(mut adler: u32, buf: &[u8]) -> u32 {
}

let mut it = buf.chunks_exact(NMAX as usize);
while let Some(big_chunk) = it.next() {
for big_chunk in it.by_ref() {
const N: usize = if UNROLL_MORE { 16 } else { 8 } as usize;
let it = big_chunk.chunks_exact(N);
for chunk in it {
Expand Down Expand Up @@ -127,7 +127,7 @@ fn adler32_len_16(mut adler: u32, buf: &[u8], mut sum2: u32) -> u32 {
fn adler32_len_64(mut adler: u32, buf: &[u8], mut sum2: u32) -> u32 {
const N: usize = if UNROLL_MORE { 16 } else { 8 };
let mut it = buf.chunks_exact(N);
while let Some(chunk) = it.next() {
for chunk in it.by_ref() {
if N == 16 {
do16!(adler, sum2, chunk);
} else {
Expand Down
11 changes: 10 additions & 1 deletion src/allocate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::ffi::{c_uint, c_void};

pub fn free_aligned(zfree: crate::c_api::free_func, opaque: *mut c_void, ptr: *mut c_void) {
/// # Safety
///
/// The `ptr` must be allocated with the allocator that the `zfree` function belongs to.
pub unsafe fn free_aligned(zfree: crate::c_api::free_func, opaque: *mut c_void, ptr: *mut c_void) {
if zfree as usize == zcfree as usize {
// safety: only unsafe because of the public interface
unsafe { zcfree(opaque, ptr) }
Expand All @@ -18,11 +21,17 @@ pub fn free_aligned(zfree: crate::c_api::free_func, opaque: *mut c_void, ptr: *m
}
}

/// # Safety
///
/// This function is safe, but must have this type signature to be used elsewhere in the library
pub unsafe extern "C" fn zcalloc(opaque: *mut c_void, items: c_uint, size: c_uint) -> *mut c_void {
let _ = opaque;
zng_alloc(items as libc::size_t * size as libc::size_t)
}

/// # Safety
///
/// The `ptr` must be allocated with the allocator that is used internally by `zcfree`
pub unsafe extern "C" fn zcfree(opaque: *mut c_void, ptr: *mut c_void) {
let _ = opaque;
zng_free(ptr)
Expand Down
4 changes: 2 additions & 2 deletions src/bitreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<'a> BitReader<'a> {

#[inline(always)]
pub fn bits_in_buffer(&self) -> u8 {
self.bits_used as u8
self.bits_used
}

#[inline(always)]
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'a> BitReader<'a> {
let read = unsafe { std::ptr::read_unaligned(self.ptr.cast::<u64>()) };

self.bit_buffer |= read << self.bits_used;
let increment = 7 - (self.bits_used >> 3) & 7;
let increment = (7 - (self.bits_used >> 3)) & 7;
self.ptr = unsafe { self.ptr.add(increment as usize) };
self.bits_used |= 56;
}
Expand Down
11 changes: 8 additions & 3 deletions src/c_api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(clippy::missing_safety_doc)] // obviously needs to be fixed long-term

use libc::{c_char, c_int, c_uchar, c_uint, c_ulong, c_void};

Expand Down Expand Up @@ -116,7 +117,7 @@ pub unsafe extern "C" fn inflate(strm: *mut z_stream, flush: i32) -> i32 {
}

pub unsafe extern "C" fn inflateEnd(strm: *mut z_stream) -> i32 {
crate::inflate::inflateEnd(strm)
crate::inflate::end(strm)
}

pub unsafe extern "C" fn inflateBackInit_(
Expand Down Expand Up @@ -160,7 +161,11 @@ pub unsafe extern "C" fn inflateMark(strm: *const z_stream) -> libc::c_long {
}

pub unsafe extern "C" fn inflateSync(strm: *mut z_stream) -> i32 {
crate::inflate::inflateSync(strm)
if let Some(stream) = InflateStream::from_stream_mut(strm) {
crate::inflate::sync(stream) as _
} else {
ReturnCode::StreamError as _
}
}

// undocumented
Expand All @@ -177,7 +182,7 @@ pub unsafe extern "C" fn inflateInit_(
_version: *const c_char,
_stream_size: c_int,
) -> c_int {
crate::inflate::inflateInit(strm)
crate::inflate::init(strm)
}

pub unsafe extern "C" fn inflateInit2_(
Expand Down
Loading