Skip to content

Commit

Permalink
implemented strlen to deref the callback message
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Jun 13, 2019
1 parent ea75907 commit 84b5b37
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,11 @@ extern "C" {
///
/// See also secp256k1_default_error_callback_fn.
///
pub extern "C" fn secp256k1_default_illegal_callback_fn(_message: *const c_char, _data: *mut c_void) {
// Do we need to deref the message and print it? if so without std we'll need to use `strlen`
panic!("[libsecp256k1] illegal argument.");
pub unsafe extern "C" fn secp256k1_default_illegal_callback_fn(message: *const c_char, _data: *mut c_void) {
use core::{str, slice};
let msg_slice = slice::from_raw_parts(message as *const u8, strlen(message));
let msg = str::from_utf8_unchecked(msg_slice);
panic!("[libsecp256k1] illegal argument. {}", msg);
}

#[no_mangle]
Expand All @@ -288,9 +290,22 @@ pub extern "C" fn secp256k1_default_illegal_callback_fn(_message: *const c_char,
///
/// See also secp256k1_default_illegal_callback_fn.
///
pub extern "C" fn secp256k1_default_error_callback_fn(_message: *const c_char, _data: *mut c_void) {
pub unsafe extern "C" fn secp256k1_default_error_callback_fn(message: *const c_char, _data: *mut c_void) {
// Do we need to deref the message and print it? if so without std we'll need to use `strlen`
panic!("[libsecp256k1] internal consistency check failed.");
use core::{str, slice};
let msg_slice = slice::from_raw_parts(message as *const u8, strlen(message));
let msg = str::from_utf8_unchecked(msg_slice);
panic!("[libsecp256k1] internal consistency check failed {}", msg);
}


unsafe fn strlen(mut str_ptr: *const c_char) -> usize {
let mut ctr = 0;
while *str_ptr != '\0' as c_char {
ctr += 1;
str_ptr = str_ptr.offset(1);
}
ctr
}


Expand Down

0 comments on commit 84b5b37

Please sign in to comment.