@@ -569,6 +569,33 @@ pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) {
569
569
rustsecp256k1_v0_4_1_context_destroy ( ctx)
570
570
}
571
571
572
+ /// FFI-safe replacement for panic
573
+ ///
574
+ /// Prints to stderr and aborts with `std`, double panics without `std`.
575
+ #[ cfg_attr( not( feature = "std" ) , allow( unused) ) ]
576
+ fn ffi_abort ( msg : impl core:: fmt:: Display ) -> ! {
577
+ #[ cfg( feature = "std" ) ]
578
+ {
579
+ eprintln ! ( "[libsecp256k1] {}" , msg) ;
580
+ std:: process:: abort ( )
581
+ }
582
+ #[ cfg( not( feature = "std" ) ) ]
583
+ {
584
+ use core:: fmt:: Display ;
585
+
586
+ // Abort by double panic
587
+ struct PanicOnDrop < M : Display > ( M ) ;
588
+
589
+ impl < T : Display > Drop for PanicOnDrop < T > {
590
+ fn drop ( & mut self ) {
591
+ panic ! ( "[libsecp256k1] {}" , self . 0 ) ;
592
+ }
593
+ }
594
+
595
+ let _bomb = PanicOnDrop ( & msg) ;
596
+ panic ! ( "[libsecp256k1] {}" , & msg)
597
+ }
598
+ }
572
599
573
600
/// **This function is an override for the C function, this is the an edited version of the original description:**
574
601
///
@@ -594,7 +621,7 @@ pub unsafe extern "C" fn rustsecp256k1_v0_4_1_default_illegal_callback_fn(messag
594
621
use core:: str;
595
622
let msg_slice = slice:: from_raw_parts ( message as * const u8 , strlen ( message) ) ;
596
623
let msg = str:: from_utf8_unchecked ( msg_slice) ;
597
- panic ! ( "[libsecp256k1] illegal argument. {}" , msg) ;
624
+ ffi_abort ( format_args ! ( "illegal argument. {}" , msg) ) ;
598
625
}
599
626
600
627
/// **This function is an override for the C function, this is the an edited version of the original description:**
@@ -617,7 +644,7 @@ pub unsafe extern "C" fn rustsecp256k1_v0_4_1_default_error_callback_fn(message:
617
644
use core:: str;
618
645
let msg_slice = slice:: from_raw_parts ( message as * const u8 , strlen ( message) ) ;
619
646
let msg = str:: from_utf8_unchecked ( msg_slice) ;
620
- panic ! ( "[libsecp256k1] internal consistency check failed {}" , msg) ;
647
+ ffi_abort ( format_args ! ( "internal consistency check failed {}" , msg) ) ;
621
648
}
622
649
623
650
#[ cfg( not( rust_secp_no_symbol_renaming) ) ]
@@ -826,7 +853,7 @@ mod fuzz_dummy {
826
853
* output = 4 ;
827
854
ptr:: copy ( ( * pk) . 0 . as_ptr ( ) , output. offset ( 1 ) , 64 ) ;
828
855
} else {
829
- panic ! ( "Bad flags" ) ;
856
+ ffi_abort ( format_args ! ( "Bad flags" ) ) ;
830
857
}
831
858
1
832
859
}
0 commit comments