@@ -442,73 +442,83 @@ static bool add_to_closest(Group_c *_Nonnull g, const uint8_t *_Nonnull real_pk,
442442 return false;
443443 }
444444
445- unsigned int index = DESIRED_CLOSEST ;
446-
447445 for (unsigned int i = 0 ; i < DESIRED_CLOSEST ; ++ i ) {
448446 if (g -> closest_peers [i ].active && pk_equal (real_pk , g -> closest_peers [i ].real_pk )) {
449447 return true;
450448 }
451449 }
452450
453- for (unsigned int i = 0 ; i < DESIRED_CLOSEST ; ++ i ) {
454- if (!g -> closest_peers [i ].active ) {
455- index = i ;
456- break ;
457- }
458- }
451+ uint8_t cur_real_pk [CRYPTO_PUBLIC_KEY_SIZE ];
452+ uint8_t cur_temp_pk [CRYPTO_PUBLIC_KEY_SIZE ];
453+ memcpy (cur_real_pk , real_pk , CRYPTO_PUBLIC_KEY_SIZE );
454+ memcpy (cur_temp_pk , temp_pk , CRYPTO_PUBLIC_KEY_SIZE );
459455
460- if (index == DESIRED_CLOSEST ) {
461- uint64_t comp_val = calculate_comp_value (g -> real_pk , real_pk );
462- uint64_t comp_d = 0 ;
456+ bool added = false;
463457
464- for ( unsigned int i = 0 ; i < ( DESIRED_CLOSEST / 2 ); ++ i ) {
465- const uint64_t comp = calculate_comp_value ( g -> real_pk , g -> closest_peers [ i ]. real_pk ) ;
458+ while (true ) {
459+ unsigned int index = DESIRED_CLOSEST ;
466460
467- if (comp > comp_val && comp > comp_d ) {
461+ for (unsigned int i = 0 ; i < DESIRED_CLOSEST ; ++ i ) {
462+ if (!g -> closest_peers [i ].active ) {
468463 index = i ;
469- comp_d = comp ;
464+ break ;
470465 }
471466 }
472467
473- comp_val = calculate_comp_value (real_pk , g -> real_pk );
468+ if (index == DESIRED_CLOSEST ) {
469+ uint64_t comp_val = calculate_comp_value (g -> real_pk , cur_real_pk );
470+ uint64_t comp_d = 0 ;
474471
475- for (unsigned int i = DESIRED_CLOSEST / 2 ; i < DESIRED_CLOSEST ; ++ i ) {
476- const uint64_t comp = calculate_comp_value (g -> closest_peers [ i ]. real_pk , g -> real_pk );
472+ for (unsigned int i = 0 ; i < ( DESIRED_CLOSEST / 2 ) ; ++ i ) {
473+ const uint64_t comp = calculate_comp_value (g -> real_pk , g -> closest_peers [ i ]. real_pk );
477474
478- if (comp > comp_val && comp > comp_d ) {
479- index = i ;
480- comp_d = comp ;
475+ if (comp > comp_val && comp > comp_d ) {
476+ index = i ;
477+ comp_d = comp ;
478+ }
481479 }
482- }
483- }
484480
485- if (index == DESIRED_CLOSEST ) {
486- return false;
487- }
481+ comp_val = calculate_comp_value (cur_real_pk , g -> real_pk );
488482
489- uint8_t old_real_pk [CRYPTO_PUBLIC_KEY_SIZE ];
490- uint8_t old_temp_pk [CRYPTO_PUBLIC_KEY_SIZE ];
491- bool old = false;
483+ for (unsigned int i = DESIRED_CLOSEST / 2 ; i < DESIRED_CLOSEST ; ++ i ) {
484+ const uint64_t comp = calculate_comp_value (g -> closest_peers [i ].real_pk , g -> real_pk );
492485
493- if (g -> closest_peers [index ].active ) {
494- memcpy (old_real_pk , g -> closest_peers [index ].real_pk , CRYPTO_PUBLIC_KEY_SIZE );
495- memcpy (old_temp_pk , g -> closest_peers [index ].temp_pk , CRYPTO_PUBLIC_KEY_SIZE );
496- old = true;
497- }
486+ if (comp > comp_val && comp > comp_d ) {
487+ index = i ;
488+ comp_d = comp ;
489+ }
490+ }
491+ }
492+
493+ if (index == DESIRED_CLOSEST ) {
494+ break ;
495+ }
498496
499- g -> closest_peers [index ].active = true;
500- memcpy (g -> closest_peers [index ].real_pk , real_pk , CRYPTO_PUBLIC_KEY_SIZE );
501- memcpy (g -> closest_peers [index ].temp_pk , temp_pk , CRYPTO_PUBLIC_KEY_SIZE );
497+ if (g -> closest_peers [index ].active ) {
498+ uint8_t tmp [CRYPTO_PUBLIC_KEY_SIZE ];
499+ memcpy (tmp , g -> closest_peers [index ].real_pk , CRYPTO_PUBLIC_KEY_SIZE );
500+ memcpy (g -> closest_peers [index ].real_pk , cur_real_pk , CRYPTO_PUBLIC_KEY_SIZE );
501+ memcpy (cur_real_pk , tmp , CRYPTO_PUBLIC_KEY_SIZE );
502502
503- if (old ) {
504- add_to_closest (g , old_real_pk , old_temp_pk );
503+ memcpy (tmp , g -> closest_peers [index ].temp_pk , CRYPTO_PUBLIC_KEY_SIZE );
504+ memcpy (g -> closest_peers [index ].temp_pk , cur_temp_pk , CRYPTO_PUBLIC_KEY_SIZE );
505+ memcpy (cur_temp_pk , tmp , CRYPTO_PUBLIC_KEY_SIZE );
506+ } else {
507+ g -> closest_peers [index ].active = true;
508+ memcpy (g -> closest_peers [index ].real_pk , cur_real_pk , CRYPTO_PUBLIC_KEY_SIZE );
509+ memcpy (g -> closest_peers [index ].temp_pk , cur_temp_pk , CRYPTO_PUBLIC_KEY_SIZE );
510+ added = true;
511+ break ;
512+ }
513+
514+ added = true;
505515 }
506516
507- if (g -> changed == GROUPCHAT_CLOSEST_CHANGE_NONE ) {
517+ if (added && g -> changed == GROUPCHAT_CLOSEST_CHANGE_NONE ) {
508518 g -> changed = GROUPCHAT_CLOSEST_CHANGE_ADDED ;
509519 }
510520
511- return true ;
521+ return added ;
512522}
513523
514524static bool pk_in_closest_peers (const Group_c * _Nonnull g , const uint8_t * _Nonnull real_pk )
0 commit comments