Skip to content

Commit 9f7236c

Browse files
committed
Added manual external callbacks that panics
1 parent d28e8a6 commit 9f7236c

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fn main() {
5454
.define("USE_SCALAR_INV_BUILTIN", Some("1"))
5555
.define("USE_ENDOMORPHISM", Some("1"))
5656
.define("ENABLE_MODULE_ECDH", Some("1"))
57+
.define("USE_EXTERNAL_DEFAULT_CALLBACKS", Some("1"))
5758
.define("ECMULT_WINDOW_SIZE", Some("15")); // This is the default in the configure file (`auto`)
5859

5960
#[cfg(feature = "recovery")]

src/ffi.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,52 @@ extern "C" {
248248
) -> c_int;
249249
}
250250

251+
252+
#[no_mangle]
253+
/// **This function is an override for the C function, this is the an edited version of the original description:**
254+
///
255+
/// A callback function to be called when an illegal argument is passed to
256+
/// an API call. It will only trigger for violations that are mentioned
257+
/// explicitly in the header. **This will cause a panic**.
258+
///
259+
/// The philosophy is that these shouldn't be dealt with through a
260+
/// specific return value, as calling code should not have branches to deal with
261+
/// the case that this code itself is broken.
262+
///
263+
/// On the other hand, during debug stage, one would want to be informed about
264+
/// such mistakes, and the default (crashing) may be inadvisable.
265+
/// When this callback is triggered, the API function called is guaranteed not
266+
/// to cause a crash, though its return value and output arguments are
267+
/// undefined.
268+
///
269+
/// See also secp256k1_default_error_callback_fn.
270+
///
271+
pub extern "C" fn secp256k1_default_illegal_callback_fn(_message: *const c_char, _data: *mut c_void) {
272+
// Do we need to deref the message and print it? if so without std we'll need to use `strlen`
273+
panic!("[libsecp256k1] illegal argument.");
274+
}
275+
276+
#[no_mangle]
277+
/// **This function is an override for the C function, this is the an edited version of the original description:**
278+
///
279+
/// A callback function to be called when an internal consistency check
280+
/// fails. **This will cause a panic**.
281+
///
282+
/// This can only trigger in case of a hardware failure, miscompilation,
283+
/// memory corruption, serious bug in the library, or other error would can
284+
/// otherwise result in undefined behaviour. It will not trigger due to mere
285+
/// incorrect usage of the API (see secp256k1_default_illegal_callback_fn
286+
/// for that). After this callback returns, anything may happen, including
287+
/// crashing.
288+
///
289+
/// See also secp256k1_default_illegal_callback_fn.
290+
///
291+
pub extern "C" fn secp256k1_default_error_callback_fn(_message: *const c_char, _data: *mut c_void) {
292+
// Do we need to deref the message and print it? if so without std we'll need to use `strlen`
293+
panic!("[libsecp256k1] internal consistency check failed.");
294+
}
295+
296+
251297
#[cfg(feature = "fuzztarget")]
252298
mod fuzz_dummy {
253299
use std::os::raw::{c_int, c_uchar, c_uint, c_void};

src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use core::fmt;
44
pub type c_int = i32;
55
pub type c_uchar = u8;
66
pub type c_uint = u32;
7+
pub type c_char = i8;
78

89
/// This is an exact copy of https://doc.rust-lang.org/core/ffi/enum.c_void.html
910
/// It should be Equivalent to C's void type when used as a pointer.

0 commit comments

Comments
 (0)