Skip to content

Commit 3addb04

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

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
@@ -601,27 +601,31 @@ 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);
610616

611617
const IP_Port ip_port_bak = node->ip_port;
612-
memcpy(node->public_key, pk, CRYPTO_PUBLIC_KEY_SIZE);
613-
614-
node->ip_port = *ip_port;
618+
memcpy(node->public_key, pk_cur, CRYPTO_PUBLIC_KEY_SIZE);
615619

616-
if (i != length - 1) {
617-
add_to_list(nodes_list, length, pk_bak, &ip_port_bak, cmp_pk);
618-
}
620+
node->ip_port = ip_port_cur;
619621

620-
return true;
622+
memcpy(pk_cur, pk_bak, CRYPTO_PUBLIC_KEY_SIZE);
623+
ip_port_cur = ip_port_bak;
624+
inserted = true;
621625
}
622626
}
623627

624-
return false;
628+
return inserted;
625629
}
626630

627631
/**

0 commit comments

Comments
 (0)