@@ -275,9 +275,11 @@ extern "C" {
275
275
///
276
276
/// See also secp256k1_default_error_callback_fn.
277
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." ) ;
278
+ pub unsafe extern "C" fn secp256k1_default_illegal_callback_fn ( message : * const c_char , _data : * mut c_void ) {
279
+ use core:: { str, slice} ;
280
+ let msg_slice = slice:: from_raw_parts ( message as * const u8 , strlen ( message) ) ;
281
+ let msg = str:: from_utf8_unchecked ( msg_slice) ;
282
+ panic ! ( "[libsecp256k1] illegal argument. {}" , msg) ;
281
283
}
282
284
283
285
#[ no_mangle]
@@ -295,9 +297,21 @@ pub extern "C" fn secp256k1_default_illegal_callback_fn(_message: *const c_char,
295
297
///
296
298
/// See also secp256k1_default_illegal_callback_fn.
297
299
///
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." ) ;
300
+ pub unsafe extern "C" fn secp256k1_default_error_callback_fn ( message : * const c_char , _data : * mut c_void ) {
301
+ use core:: { str, slice} ;
302
+ let msg_slice = slice:: from_raw_parts ( message as * const u8 , strlen ( message) ) ;
303
+ let msg = str:: from_utf8_unchecked ( msg_slice) ;
304
+ panic ! ( "[libsecp256k1] internal consistency check failed {}" , msg) ;
305
+ }
306
+
307
+
308
+ unsafe fn strlen ( mut str_ptr : * const c_char ) -> usize {
309
+ let mut ctr = 0 ;
310
+ while * str_ptr != '\0' as c_char {
311
+ ctr += 1 ;
312
+ str_ptr = str_ptr. offset ( 1 ) ;
313
+ }
314
+ ctr
301
315
}
302
316
303
317
@@ -648,3 +662,18 @@ mod fuzz_dummy {
648
662
}
649
663
#[ cfg( feature = "fuzztarget" ) ]
650
664
pub use self :: fuzz_dummy:: * ;
665
+
666
+
667
+ #[ cfg( test) ]
668
+ mod tests {
669
+ use std:: ffi:: CString ;
670
+ use super :: strlen;
671
+
672
+ #[ test]
673
+ fn test_strlen ( ) {
674
+ let orig = "test strlen \t \n " ;
675
+ let test = CString :: new ( orig) . unwrap ( ) ;
676
+
677
+ assert_eq ! ( orig. len( ) , unsafe { strlen( test. as_ptr( ) ) } ) ;
678
+ }
679
+ }
0 commit comments