Skip to content

Commit f7a4a7e

Browse files
committed
Added manual external callbacks that panics
1 parent 0b770cf commit f7a4a7e

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
@@ -53,6 +53,7 @@ fn main() {
5353
.define("USE_FIELD_INV_BUILTIN", Some("1"))
5454
.define("USE_SCALAR_INV_BUILTIN", Some("1"))
5555
.define("ENABLE_MODULE_ECDH", Some("1"))
56+
.define("USE_EXTERNAL_DEFAULT_CALLBACKS", Some("1"))
5657
.define("ECMULT_WINDOW_SIZE", Some("15")); // This is the default in the configure file (`auto`)
5758

5859
#[cfg(feature = "endomorphism")]

src/ffi.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,52 @@ extern "C" {
255255
) -> c_int;
256256
}
257257

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

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)