Skip to content

Commit 0cf065c

Browse files
committed
refactor: Make add_to_list non-recursive.
1 parent 7cefa93 commit 0cf065c

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

toxcore/DHT.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -606,27 +606,31 @@ bool add_to_list(
606606
Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE],
607607
const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE])
608608
{
609+
uint8_t pk_cur[CRYPTO_PUBLIC_KEY_SIZE];
610+
memcpy(pk_cur, pk, CRYPTO_PUBLIC_KEY_SIZE);
611+
IP_Port ip_port_cur = *ip_port;
612+
613+
bool inserted = false;
614+
609615
for (uint32_t i = 0; i < length; ++i) {
610616
Node_format *node = &nodes_list[i];
611617

612-
if (id_closest(cmp_pk, node->public_key, pk) == 2) {
618+
if (id_closest(cmp_pk, node->public_key, pk_cur) == 2) {
613619
uint8_t pk_bak[CRYPTO_PUBLIC_KEY_SIZE];
614620
memcpy(pk_bak, node->public_key, CRYPTO_PUBLIC_KEY_SIZE);
615621

616622
const IP_Port ip_port_bak = node->ip_port;
617-
memcpy(node->public_key, pk, CRYPTO_PUBLIC_KEY_SIZE);
618-
619-
node->ip_port = *ip_port;
623+
memcpy(node->public_key, pk_cur, CRYPTO_PUBLIC_KEY_SIZE);
620624

621-
if (i != length - 1) {
622-
add_to_list(nodes_list, length, pk_bak, &ip_port_bak, cmp_pk);
623-
}
625+
node->ip_port = ip_port_cur;
624626

625-
return true;
627+
memcpy(pk_cur, pk_bak, CRYPTO_PUBLIC_KEY_SIZE);
628+
ip_port_cur = ip_port_bak;
629+
inserted = true;
626630
}
627631
}
628632

629-
return false;
633+
return inserted;
630634
}
631635

632636
/**

0 commit comments

Comments
 (0)