Skip to content

Commit e115b13

Browse files
committed
refactor: Make add_to_list non-recursive.
1 parent 206ea35 commit e115b13

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

toxcore/DHT.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -601,27 +601,30 @@ bool add_to_list(
601601
Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE],
602602
const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE])
603603
{
604+
uint8_t pk_cur[CRYPTO_PUBLIC_KEY_SIZE];
605+
memcpy(pk_cur, pk, CRYPTO_PUBLIC_KEY_SIZE);
606+
IP_Port ip_port_cur = *ip_port;
607+
608+
bool inserted = false;
609+
604610
for (uint32_t i = 0; i < length; ++i) {
605611
Node_format *node = &nodes_list[i];
606612

607-
if (id_closest(cmp_pk, node->public_key, pk) == 2) {
613+
if (id_closest(cmp_pk, node->public_key, pk_cur) == 2) {
608614
uint8_t pk_bak[CRYPTO_PUBLIC_KEY_SIZE];
609615
memcpy(pk_bak, node->public_key, CRYPTO_PUBLIC_KEY_SIZE);
610-
611616
const IP_Port ip_port_bak = node->ip_port;
612-
memcpy(node->public_key, pk, CRYPTO_PUBLIC_KEY_SIZE);
613617

614-
node->ip_port = *ip_port;
618+
memcpy(node->public_key, pk_cur, CRYPTO_PUBLIC_KEY_SIZE);
619+
node->ip_port = ip_port_cur;
615620

616-
if (i != length - 1) {
617-
add_to_list(nodes_list, length, pk_bak, &ip_port_bak, cmp_pk);
618-
}
619-
620-
return true;
621+
memcpy(pk_cur, pk_bak, CRYPTO_PUBLIC_KEY_SIZE);
622+
ip_port_cur = ip_port_bak;
623+
inserted = true;
621624
}
622625
}
623626

624-
return false;
627+
return inserted;
625628
}
626629

627630
/**

0 commit comments

Comments
 (0)