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

Use panics in handle_assert_failure too when stdio mode is disabled #68

Merged
merged 2 commits into from
Dec 11, 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
3 changes: 2 additions & 1 deletion libbz2-rs-sys-cdylib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ license = "bzip2-1.0.6"
repository = "https://github.com/trifectatechfoundation/libbzip2-rs"
homepage = "https://github.com/trifectatechfoundation/libbzip2-rs"
readme = "./README.md"
description = "a drop-in compatible libbz2 cdylib"
description = "a drop-in compatible libbz2 cdylib"
publish = true
rust-version = "1.82" # MSRV

[lib]
name = "bz2_rs" # turns into e.g. `libbz2_rs.so`
crate-type = ["cdylib"]
test = false
bench = false

[features]
default = ["stdio"]
Expand Down
6 changes: 5 additions & 1 deletion libbz2-rs-sys-cdylib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ fn panic_handler(_info: &PanicInfo) -> ! {

#[cfg(not(feature = "stdio"))]
{
use core::sync::atomic::Ordering;

extern "C" {
fn bz_internal_error(errcode: core::ffi::c_int);
}

unsafe { bz_internal_error(-1) }
// If the panic was triggered by handle_assert_failure ASSERT_CODE will contain the
// assertion code. Otherwise it will contain -1.
unsafe { bz_internal_error(ASSERT_CODE.load(Ordering::Relaxed)) }
loop {}
}
}
37 changes: 17 additions & 20 deletions libbz2-rs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
extern crate std;

use core::ffi::c_int;
#[cfg(not(feature = "std"))]
use core::sync::atomic::{AtomicI32, Ordering};

mod allocator;
mod blocksort;
Expand Down Expand Up @@ -135,28 +137,23 @@
};
}

#[cfg(not(feature = "std"))]
#[doc(hidden)]
pub static ASSERT_CODE: AtomicI32 = AtomicI32::new(-1);

#[cold]
fn handle_assert_failure(errcode: c_int) -> ! {
#[cfg(feature = "stdio")]
{
#[cfg(feature = "std")]
std::eprint!("{}", AssertFail(errcode));
#[cfg(feature = "std")]
std::process::exit(3);

#[cfg(not(feature = "std"))]
panic!("{}", AssertFail(errcode));
}

#[cfg(not(feature = "stdio"))]
{
extern "C" {
fn bz_internal_error(errcode: c_int);
}

unsafe { bz_internal_error(errcode) }
loop {}
}
#[cfg(feature = "std")]
std::eprint!("{}", AssertFail(errcode));
#[cfg(feature = "std")]
std::process::exit(3);

Check warning on line 149 in libbz2-rs-sys/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

libbz2-rs-sys/src/lib.rs#L146-L149

Added lines #L146 - L149 were not covered by tests

// Stash the assertion code for the panic handler in the cdylib to pass to bz_internal_error.
// Using relaxed ordering as this will be accessed on the same thread.
#[cfg(not(feature = "std"))]
ASSERT_CODE.store(errcode as i32, Ordering::Relaxed);
#[cfg(not(feature = "std"))]
panic!("{}", AssertFail(errcode));
}

use assert_h;
Expand Down
Loading