@@ -38,7 +38,8 @@ pub mod types;
38
38
#[ cfg_attr( docsrs, doc( cfg( feature = "recovery" ) ) ) ]
39
39
pub mod recovery;
40
40
41
- use core:: { slice, ptr} ;
41
+ use core:: ffi:: CStr ;
42
+ use core:: ptr;
42
43
use types:: * ;
43
44
44
45
/// Flag for context to enable no precomputation
@@ -864,17 +865,17 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_destroy(ctx: *mut Context)
864
865
///
865
866
/// See also secp256k1_default_error_callback_fn.
866
867
///
867
- ///
868
868
/// # Safety
869
869
///
870
- /// For safety constraints see [`std::slice::from_raw_parts`] and [`std::str::from_utf8_unchecked`] .
870
+ /// `message` must point to a valid C string (see safety constraints on [`core::ffi::CStr::from_ptr`]) .
871
871
#[ no_mangle]
872
872
#[ cfg( not( rust_secp_no_symbol_renaming) ) ]
873
873
pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_illegal_callback_fn ( message : * const c_char , _data : * mut c_void ) {
874
- use core:: str;
875
- let msg_slice = slice:: from_raw_parts ( message as * const u8 , strlen ( message) ) ;
876
- let msg = str:: from_utf8_unchecked ( msg_slice) ;
877
- panic ! ( "[libsecp256k1] illegal argument. {}" , msg) ;
874
+ let s = CStr :: from_ptr ( message) ;
875
+ match s. to_str ( ) {
876
+ Ok ( msg) => panic ! ( "[libsecp256k1] illegal argument. {}" , msg) ,
877
+ Err ( _) => panic ! ( "[libsecp256k1] illegal argument (msg elided due to invalid UTF-8)" ) ,
878
+ }
878
879
}
879
880
880
881
/// **This function is an override for the C function, this is the an edited version of the original description:**
@@ -893,33 +894,17 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_illegal_callback_fn(messag
893
894
///
894
895
/// # Safety
895
896
///
896
- /// `message` must be valid pointer and point to a valid null terminated C string. For further
897
- /// safety constraints see [`std::slice::from_raw_parts`] and [`std::str::from_utf8_unchecked`].
897
+ /// `message` must point to a valid C string (see safety constraints on [`core::ffi::CStr::from_ptr`]).
898
898
#[ no_mangle]
899
899
#[ cfg( not( rust_secp_no_symbol_renaming) ) ]
900
900
pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_error_callback_fn ( message : * const c_char , _data : * mut c_void ) {
901
- use core:: str;
902
- let msg_slice = slice:: from_raw_parts ( message as * const u8 , strlen ( message) ) ;
903
- let msg = str:: from_utf8_unchecked ( msg_slice) ;
904
- panic ! ( "[libsecp256k1] internal consistency check failed {}" , msg) ;
905
- }
906
-
907
- /// Returns the length of the `str_ptr` string.
908
- ///
909
- /// # Safety
910
- ///
911
- /// `str_ptr` must be valid pointer and point to a valid null terminated C string.
912
- #[ cfg( not( rust_secp_no_symbol_renaming) ) ]
913
- unsafe fn strlen ( mut str_ptr : * const c_char ) -> usize {
914
- let mut ctr = 0 ;
915
- while * str_ptr != '\0' as c_char {
916
- ctr += 1 ;
917
- str_ptr = str_ptr. offset ( 1 ) ;
901
+ let s = CStr :: from_ptr ( message) ;
902
+ match s. to_str ( ) {
903
+ Ok ( msg) => panic ! ( "[libsecp256k1] internal consistency check failed {}" , msg) ,
904
+ Err ( _) => panic ! ( "[libsecp256k1] internal consistency check failed (msg elided due to invalid UTF-8)" ) ,
918
905
}
919
- ctr
920
906
}
921
907
922
-
923
908
/// A trait for producing pointers that will always be valid in C (assuming NULL pointer is a valid
924
909
/// no-op).
925
910
///
0 commit comments