From 1c57a3a3de376c8e9f127c409032fd94298e5e7b Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Tue, 17 Sep 2013 01:08:57 +0200 Subject: Tests of state loading/saving lead to two fixes for DHT.c and util.c util.c: - fix in empty section at the end of the state, showed as bug when having an empty name DHT.c: - fix in saving less data than originally announced, showed as bug when not having reached any clients ever (no clients or only with timestamp of zero) --- toxcore/DHT.c | 14 ++++++-------- toxcore/Messenger.c | 3 ++- toxcore/util.c | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) (limited to 'toxcore') diff --git a/toxcore/DHT.c b/toxcore/DHT.c index ddd1e1e0..f38ce3a5 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -1576,18 +1576,16 @@ void DHT_save(DHT *dht, uint8_t *data) if (dht->close_clientlist[i].timestamp != 0) num++; - if (!num) - return; - len = num * sizeof(Client_data); type = DHT_STATE_TYPE_CLIENTS; data = z_state_save_subheader(data, len, type); - Client_data *clients = (Client_data *)data; - - for (num = 0, i = 0; i < LCLIENT_LIST; ++i) - if (dht->close_clientlist[i].timestamp != 0) - memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); + if (num) { + Client_data *clients = (Client_data *)data; + for (num = 0, i = 0; i < LCLIENT_LIST; ++i) + if (dht->close_clientlist[i].timestamp != 0) + memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); + } data += len; } diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index cdc133d0..7ab4a92e 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1483,7 +1483,7 @@ uint32_t Messenger_size(Messenger *m) { uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; return size32 * 2 // global cookie - + sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + + sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizesubhead + DHT_size(m->dht) // DHT + sizesubhead + sizeof(Friend) * m->numfriends // Friendlist itself. + sizesubhead + m->name_length // Own nickname. @@ -1595,6 +1595,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n", length, type); #endif + break; } return 0; diff --git a/toxcore/util.c b/toxcore/util.c index 51b31e7b..b3263d05 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -58,7 +58,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer, uint32_t length_sub, cookie_type; uint32_t size32 = sizeof(uint32_t), size_head = size32 * 2; - while (length > size_head) { + while (length >= size_head) { length_sub = *(uint32_t *)data; cookie_type = *(uint32_t *)(data + size32); data += size_head; @@ -89,7 +89,7 @@ int load_state(load_state_callback_func load_state_callback, void *outer, length -= length_sub; } - return length == 0 ? 0 : - 1; + return length == 0 ? 0 : -1; }; #ifdef LOGGING -- cgit v1.2.3