Skip to content

Commit

Permalink
cleanup the assert logic
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Nov 8, 2024
1 parent 8c499ac commit cfc6d58
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 70 deletions.
63 changes: 2 additions & 61 deletions bzlib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use core::ffi::{c_char, c_int, c_uint, c_void, CStr};

use libc::FILE;
use libc::{
exit, fclose, fdopen, ferror, fflush, fgetc, fopen, fread, free, fwrite, malloc, ungetc,
};
use libc::{fclose, fdopen, ferror, fflush, fgetc, fopen, fread, free, fwrite, malloc, ungetc};

use crate::compress::BZ2_compressBlock;
use crate::crctable::BZ2_CRC32TABLE;
use crate::decompress::{self, BZ2_decompress};
use crate::libbzip2_rs_sys_version;

extern "C" {
static stdin: *mut FILE;
Expand Down Expand Up @@ -75,12 +74,6 @@ macro_rules! prefix {
};
}

macro_rules! libbzip2_rs_sys_version {
() => {
concat!("1.1.0-libbzip2-rs-sys-", env!("CARGO_PKG_VERSION"))
};
}

const LIBBZIP2_RS_SYS_VERSION: &str = concat!(libbzip2_rs_sys_version!(), "\0");

/// The version of the zlib library.
Expand Down Expand Up @@ -489,58 +482,6 @@ pub struct bzFile {
pub initialisedOk: bool,
}

pub fn BZ2_bz__AssertH__fail(errcode: libc::c_int) {
eprint!(
concat!(
"\n",
"\n",
"bzip2/libbzip2: internal error number {}.\n",
"This is a bug in bzip2/libbzip2, {}.\n",
"Please report it at: https://gitlab.com/bzip2/bzip2/-/issues\n",
"If this happened when you were using some program which uses\n",
"libbzip2 as a component, you should also report this bug to\n",
"the author(s) of that program.\n",
"Please make an effort to report this bug;\n",
"timely and accurate bug reports eventually lead to higher\n",
"quality software. Thanks.\n",
"\n"
),
errcode,
libbzip2_rs_sys_version!(),
);
if errcode == 1007 as libc::c_int {
eprint!(concat!(
"\n",
"*** A special note about internal error number 1007 ***\n",
"\n",
"Experience suggests that a common cause of i.e. 1007\n",
"is unreliable memory or other hardware. The 1007 assertion\n",
"just happens to cross-check the results of huge numbers of\n",
"memory reads/writes, and so acts (unintendedly) as a stress\n",
"test of your memory system.\n",
"\n",
"I suggest the following: try compressing the file again,\n",
"possibly monitoring progress in detail with the -vv flag.\n",
"\n",
"* If the error cannot be reproduced, and/or happens at different\n",
" points in compression, you may have a flaky memory system.\n",
" Try a memory-test program. I have used Memtest86\n",
" (www.memtest86.com). At the time of writing it is free (GPLd).\n",
" Memtest86 tests memory much more thorougly than your BIOSs\n",
" power-on test, and may find failures that the BIOS doesn't.\n",
"\n",
"* If the error can be repeatably reproduced, this is a bug in\n",
" bzip2, and I would very much like to hear about it. Please\n",
" let me know, and, ideally, save a copy of the file causing the\n",
" problem -- without which I will be unable to investigate it.\n",
"\n"
));
}
unsafe {
exit(3 as libc::c_int);
}
}

const _C_INT_SIZE: () = assert!(core::mem::size_of::<core::ffi::c_int>() == 4);
const _C_SHORT_SIZE: () = assert!(core::mem::size_of::<core::ffi::c_short>() == 2);
const _C_CHAR_SIZE: () = assert!(core::mem::size_of::<core::ffi::c_char>() == 1);
Expand Down
99 changes: 90 additions & 9 deletions c2rust-lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ mod decompress;
mod huffman;
mod randtable;

#[macro_export]
macro_rules! assert_h {
($condition:expr, $errcode:expr) => {{
if !$condition {
$crate::bzlib::BZ2_bz__AssertH__fail($errcode);
}
}};
}

pub(crate) use bzlib::{Action, ReturnCode};

pub const BZ_OK: c_int = ReturnCode::BZ_OK as c_int;
Expand Down Expand Up @@ -66,3 +57,93 @@ pub use bzlib::{
BZ2_bzclose, BZ2_bzdopen, BZ2_bzerror, BZ2_bzflush, BZ2_bzlibVersion, BZ2_bzopen, BZ2_bzread,
BZ2_bzwrite,
};

// --- version number logic

macro_rules! libbzip2_rs_sys_version {
() => {
concat!("1.1.0-libbzip2-rs-sys-", env!("CARGO_PKG_VERSION"))
};
}

pub(crate) use libbzip2_rs_sys_version;

// --- assert failure logic

macro_rules! assert_h {
($condition:expr, $errcode:expr) => {{
if !$condition {
eprint!("{}", $crate::AssertFail($errcode));
std::process::exit(3);
}
}};
}

pub(crate) use assert_h;

pub(crate) struct AssertFail(i32);

impl core::fmt::Display for AssertFail {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
concat!(
"\n",
"\n",
"bzip2/libbzip2: internal error number {}.\n",
"This is a bug in bzip2/libbzip2, {}.\n",
"Please report it at: https://gitlab.com/bzip2/bzip2/-/issues\n",
"If this happened when you were using some program which uses\n",
"libbzip2 as a component, you should also report this bug to\n",
"the author(s) of that program.\n",
"Please make an effort to report this bug;\n",
"timely and accurate bug reports eventually lead to higher\n",
"quality software. Thanks.\n",
"\n"
),
self.0,
libbzip2_rs_sys_version!(),
))?;

if self.0 == 1007 {
f.write_fmt(format_args!(concat!(
"\n",
"*** A special note about internal error number 1007 ***\n",
"\n",
"Experience suggests that a common cause of i.e. 1007\n",
"is unreliable memory or other hardware. The 1007 assertion\n",
"just happens to cross-check the results of huge numbers of\n",
"memory reads/writes, and so acts (unintendedly) as a stress\n",
"test of your memory system.\n",
"\n",
"I suggest the following: try compressing the file again,\n",
"possibly monitoring progress in detail with the -vv flag.\n",
"\n",
"* If the error cannot be reproduced, and/or happens at different\n",
" points in compression, you may have a flaky memory system.\n",
" Try a memory-test program. I have used Memtest86\n",
" (www.memtest86.com). At the time of writing it is free (GPLd).\n",
" Memtest86 tests memory much more thorougly than your BIOSs\n",
" power-on test, and may find failures that the BIOS doesn't.\n",
"\n",
"* If the error can be repeatably reproduced, this is a bug in\n",
" bzip2, and I would very much like to hear about it. Please\n",
" let me know, and, ideally, save a copy of the file causing the\n",
" problem -- without which I will be unable to investigate it.\n",
"\n"
)))?;
}

Check warning on line 134 in c2rust-lib.rs

View check run for this annotation

Codecov / codecov/patch

c2rust-lib.rs#L134

Added line #L134 was not covered by tests

Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn print_assert_fail_coverage() {
use core::fmt::Write;
write!(&mut String::new(), "{}", AssertFail(1007)).unwrap();
}
}

0 comments on commit cfc6d58

Please sign in to comment.