Skip to content

Commit 84b5b37

Browse files
committed
implemented strlen to deref the callback message
1 parent ea75907 commit 84b5b37

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/ffi.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,11 @@ extern "C" {
268268
///
269269
/// See also secp256k1_default_error_callback_fn.
270270
///
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.");
271+
pub unsafe extern "C" fn secp256k1_default_illegal_callback_fn(message: *const c_char, _data: *mut c_void) {
272+
use core::{str, slice};
273+
let msg_slice = slice::from_raw_parts(message as *const u8, strlen(message));
274+
let msg = str::from_utf8_unchecked(msg_slice);
275+
panic!("[libsecp256k1] illegal argument. {}", msg);
274276
}
275277

276278
#[no_mangle]
@@ -288,9 +290,22 @@ pub extern "C" fn secp256k1_default_illegal_callback_fn(_message: *const c_char,
288290
///
289291
/// See also secp256k1_default_illegal_callback_fn.
290292
///
291-
pub extern "C" fn secp256k1_default_error_callback_fn(_message: *const c_char, _data: *mut c_void) {
293+
pub unsafe extern "C" fn secp256k1_default_error_callback_fn(message: *const c_char, _data: *mut c_void) {
292294
// 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.");
295+
use core::{str, slice};
296+
let msg_slice = slice::from_raw_parts(message as *const u8, strlen(message));
297+
let msg = str::from_utf8_unchecked(msg_slice);
298+
panic!("[libsecp256k1] internal consistency check failed {}", msg);
299+
}
300+
301+
302+
unsafe fn strlen(mut str_ptr: *const c_char) -> usize {
303+
let mut ctr = 0;
304+
while *str_ptr != '\0' as c_char {
305+
ctr += 1;
306+
str_ptr = str_ptr.offset(1);
307+
}
308+
ctr
294309
}
295310

296311

0 commit comments

Comments
 (0)