20
20
use core:: { fmt, str} ;
21
21
22
22
use super :: { from_hex, Secp256k1 } ;
23
- use super :: Error :: { self , InvalidPublicKey , InvalidSecretKey } ;
23
+ use super :: Error :: { self , InvalidPublicKey , InvalidPublicKeySum , InvalidSecretKey } ;
24
24
use Signing ;
25
25
use Verification ;
26
26
use constants;
@@ -425,12 +425,16 @@ impl PublicKey {
425
425
426
426
/// Adds the keys in the provided slice together, returning the sum. Returns
427
427
/// an error if the result would be the point at infinity, i.e. we are adding
428
- /// a point to its own negation
428
+ /// a point to its own negation, if the provided slice has no element in it,
429
+ /// or if the number of element it contains is greater than i32::MAX.
429
430
pub fn combine_keys ( keys : & [ & PublicKey ] ) -> Result < PublicKey , Error > {
430
431
use core:: mem:: transmute;
431
432
use core:: i32:: MAX ;
432
433
433
- debug_assert ! ( keys. len( ) < MAX as usize ) ;
434
+ if keys. is_empty ( ) || keys. len ( ) > MAX as usize {
435
+ return Err ( InvalidPublicKeySum ) ;
436
+ }
437
+
434
438
unsafe {
435
439
let mut ret = ffi:: PublicKey :: new ( ) ;
436
440
let ptrs : & [ * const ffi:: PublicKey ] =
@@ -444,7 +448,7 @@ impl PublicKey {
444
448
{
445
449
Ok ( PublicKey ( ret) )
446
450
} else {
447
- Err ( InvalidPublicKey )
451
+ Err ( InvalidPublicKeySum )
448
452
}
449
453
}
450
454
}
@@ -923,6 +927,11 @@ mod test {
923
927
assert_eq ! ( sum1. unwrap( ) , exp_sum) ;
924
928
}
925
929
930
+ #[ cfg_attr( not( fuzzing) , test) ]
931
+ fn pubkey_combine_keys_empty_slice ( ) {
932
+ assert ! ( PublicKey :: combine_keys( & [ ] ) . is_err( ) ) ;
933
+ }
934
+
926
935
#[ test]
927
936
fn create_pubkey_combine ( ) {
928
937
let s = Secp256k1 :: new ( ) ;
0 commit comments