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;
@@ -395,12 +395,16 @@ impl PublicKey {
395
395
396
396
/// Adds the keys in the provided slice together, returning the sum. Returns
397
397
/// an error if the result would be the point at infinity, i.e. we are adding
398
- /// a point to its own negation
398
+ /// a point to its own negation, if the provided slice has no element in it,
399
+ /// or if the number of element it contains is greater than i32::MAX.
399
400
pub fn combine_keys ( keys : & [ & PublicKey ] ) -> Result < PublicKey , Error > {
400
401
use core:: mem:: transmute;
401
402
use core:: i32:: MAX ;
402
403
403
- debug_assert ! ( keys. len( ) < MAX as usize ) ;
404
+ if keys. is_empty ( ) || keys. len ( ) > MAX as usize {
405
+ return Err ( InvalidPublicKeySum ) ;
406
+ }
407
+
404
408
unsafe {
405
409
let mut ret = ffi:: PublicKey :: new ( ) ;
406
410
let ptrs : & [ * const ffi:: PublicKey ] =
@@ -414,7 +418,7 @@ impl PublicKey {
414
418
{
415
419
Ok ( PublicKey ( ret) )
416
420
} else {
417
- Err ( InvalidPublicKey )
421
+ Err ( InvalidPublicKeySum )
418
422
}
419
423
}
420
424
}
@@ -893,6 +897,11 @@ mod test {
893
897
assert_eq ! ( sum1. unwrap( ) , exp_sum) ;
894
898
}
895
899
900
+ #[ cfg_attr( not( fuzzing) , test) ]
901
+ fn pubkey_combine_keys_empty_slice ( ) {
902
+ assert ! ( PublicKey :: combine_keys( & [ ] ) . is_err( ) ) ;
903
+ }
904
+
896
905
#[ test]
897
906
fn create_pubkey_combine ( ) {
898
907
let s = Secp256k1 :: new ( ) ;
0 commit comments