From 3addb0451dafc48979d900ae653f81f9f187b3a7 Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 14 Feb 2024 21:53:26 +0000 Subject: [PATCH] refactor: Make add_to_list non-recursive. --- toxcore/DHT.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 58341bdf8a..4bf027ba3f 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -601,27 +601,31 @@ bool add_to_list( Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE], const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE]) { + uint8_t pk_cur[CRYPTO_PUBLIC_KEY_SIZE]; + memcpy(pk_cur, pk, CRYPTO_PUBLIC_KEY_SIZE); + IP_Port ip_port_cur = *ip_port; + + bool inserted = false; + for (uint32_t i = 0; i < length; ++i) { Node_format *node = &nodes_list[i]; - if (id_closest(cmp_pk, node->public_key, pk) == 2) { + if (id_closest(cmp_pk, node->public_key, pk_cur) == 2) { uint8_t pk_bak[CRYPTO_PUBLIC_KEY_SIZE]; memcpy(pk_bak, node->public_key, CRYPTO_PUBLIC_KEY_SIZE); const IP_Port ip_port_bak = node->ip_port; - memcpy(node->public_key, pk, CRYPTO_PUBLIC_KEY_SIZE); - - node->ip_port = *ip_port; + memcpy(node->public_key, pk_cur, CRYPTO_PUBLIC_KEY_SIZE); - if (i != length - 1) { - add_to_list(nodes_list, length, pk_bak, &ip_port_bak, cmp_pk); - } + node->ip_port = ip_port_cur; - return true; + memcpy(pk_cur, pk_bak, CRYPTO_PUBLIC_KEY_SIZE); + ip_port_cur = ip_port_bak; + inserted = true; } } - return false; + return inserted; } /**