michael-f-bryan / chmlib Goto Github PK
View Code? Open in Web Editor NEWRust bindings to the CHMLib C library
License: GNU Lesser General Public License v2.1
Rust bindings to the CHMLib C library
License: GNU Lesser General Public License v2.1
To illustrate the issue:
/*
[dependencies]
chmlib = { path = "chmlib/chmlib" }
*/
use chmlib::{ChmFile, Filter};
struct PanicOnDrop;
impl Drop for PanicOnDrop {
fn drop(&mut self) {
panic!("boom!");
}
}
fn main() {
let mut file = ChmFile::open("topics.classic.chm").unwrap();
let bomb = PanicOnDrop;
file.for_each(Filter::all(), move |_, _| {
let _ = &bomb;
Err("")
})
.unwrap();
}
$ RUST_BACKTRACE=1 cargo run
Compiling wat v0.1.0 (/home/lm978/wat)
Finished dev [unoptimized + debuginfo] target(s) in 0.18s
Running `target/debug/wat`
thread 'main' panicked at 'boom!', src/main.rs:12:9
stack backtrace:
0: rust_begin_unwind
at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:584:5
1: core::panicking::panic_fmt
at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/panicking.rs:143:14
2: <wat::PanicOnDrop as core::ops::drop::Drop>::drop
at ./src/main.rs:12:9
3: core::ptr::drop_in_place<wat::PanicOnDrop>
at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/ptr/mod.rs:448:1
4: core::ptr::drop_in_place<wat::main::{{closure}}>
at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/ptr/mod.rs:448:1
5: core::ptr::drop_in_place<chmlib::WrapperState<wat::main::{{closure}}>>
at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/ptr/mod.rs:448:1
6: chmlib::function_wrapper
at ./chmlib/chmlib/src/lib.rs:196:13
7: chm_enumerate
at /home/lm978/.cargo/registry/src/github.com-1ecc6299db9ec823/chmlib-sys-1.0.0/vendor/CHMLib/src/chm_lib.c:1709:31
8: chmlib::ChmFile::for_each
at ./chmlib/chmlib/src/lib.rs:69:23
9: wat::main
at ./src/main.rs:19:5
10: core::ops::function::FnOnce::call_once
at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Notice how the panic unwinds through chm_enumerate
. To quote the Nomicon, this is UB:
Rust's unwinding strategy is not specified to be fundamentally compatible with any other language's unwinding. As such, unwinding into Rust from another language, or unwinding into another language from Rust is Undefined Behavior.
The problem comes from the handling of the WrapperState
, in particular, these two lines in function_wrapper()
:
Line 196 in 4ba9f1b
Line 201 in 4ba9f1b
These both drop the callback previously contained in WrapperState::Closure()
, outside the catch_unwind()
boundary. If the callback panics on drop, then it will unwind into C code.
There are two trivial fixes for this:
WrapperState
was changed to an enum
.WrapperState::new(cb)
, write WrapperState::new(&mut cb)
to store the callback by reference.A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.