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 Jul 3, 2019
1 parent 8022757 commit 68c5c5e
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,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 @@ -295,9 +297,21 @@ 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) {
// 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.");
pub unsafe extern "C" fn secp256k1_default_error_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] 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 Expand Up @@ -648,3 +662,18 @@ mod fuzz_dummy {
}
#[cfg(feature = "fuzztarget")]
pub use self::fuzz_dummy::*;


#[cfg(test)]
mod tests {
use std::ffi::CString;
use super::strlen;

#[test]
fn test_strlen() {
let orig = "test strlen \t \n";
let test = CString::new(orig).unwrap();

assert_eq!(orig.len(), unsafe {strlen(test.as_ptr())});
}
}

0 comments on commit 68c5c5e

Please sign in to comment.