From ebaa4fac0cfc3ae3a00140474c38b1e0b3b53178 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 21 Feb 2019 11:58:05 +0100 Subject: [PATCH] fix uninit issue: bad use of hash map --- src/rps/gnunet-service-rps.c | 22 +++++++++++---- src/rps/rps-test_util.c | 53 ++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c index e3e25342e..ccf54d1e2 100644 --- a/src/rps/gnunet-service-rps.c +++ b/src/rps/gnunet-service-rps.c @@ -2948,7 +2948,11 @@ destroy_sub (struct Sub *sub) for (uint32_t i = 0; i < 256; i++) { char push_recv_str_tmp[8]; - (void) snprintf (push_recv_str_tmp, 8, "%" PRIu32 "\n", sub->push_recv[i]); + + GNUNET_snprintf (push_recv_str_tmp, + sizeof (push_recv_str_tmp), + "%" PRIu32 "\n", + sub->push_recv[i]); (void) strncat (push_recv_str, push_recv_str_tmp, 1535 - strnlen (push_recv_str, 1536)); @@ -2956,7 +2960,8 @@ destroy_sub (struct Sub *sub) (void) strncat (push_recv_str, "\n", 1535 - strnlen (push_recv_str, 1536)); - LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing push stats to disk\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Writing push stats to disk\n"); to_file_w_len (sub->file_name_push_recv, 1535, push_recv_str); GNUNET_free (sub->file_name_push_recv); sub->file_name_push_recv = NULL; @@ -2965,7 +2970,11 @@ destroy_sub (struct Sub *sub) for (uint32_t i = 0; i < 256; i++) { char pull_delays_str_tmp[8]; - (void) snprintf (pull_delays_str_tmp, 8, "%" PRIu32 "\n", sub->pull_delays[i]); + + GNUNET_snprintf (pull_delays_str_tmp, + sizeof (pull_delays_str_tmp), + "%" PRIu32 "\n", + sub->pull_delays[i]); (void) strncat (pull_delays_str, pull_delays_str_tmp, 1535 - strnlen (pull_delays_str, 1536)); @@ -3028,8 +3037,11 @@ core_connects (void *cls, (void) cls; (void) mq; - GNUNET_CONTAINER_multipeermap_put (map_single_hop, peer, NULL, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); + GNUNET_assert (GNUNET_YES == + GNUNET_CONTAINER_multipeermap_put (map_single_hop, + peer, + NULL, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); return NULL; } diff --git a/src/rps/rps-test_util.c b/src/rps/rps-test_util.c index 3a482c1e6..85829f247 100644 --- a/src/rps/rps-test_util.c +++ b/src/rps/rps-test_util.c @@ -74,49 +74,42 @@ static struct GNUNET_CONTAINER_MultiHashMap *open_files; struct GNUNET_DISK_FileHandle * get_file_handle (const char *name) { - struct GNUNET_HashCode hash = {0}; + struct GNUNET_HashCode hash; struct GNUNET_DISK_FileHandle *fh; if (NULL == open_files) { open_files = GNUNET_CONTAINER_multihashmap_create (16, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); + GNUNET_NO); LOG (GNUNET_ERROR_TYPE_DEBUG, "Created map of open files.\n"); } GNUNET_CRYPTO_hash (name, - strnlen (name, - 512), + strlen (name), &hash); - if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (open_files, - &hash)) - { - fh = GNUNET_DISK_file_open (name, - GNUNET_DISK_OPEN_WRITE | - GNUNET_DISK_OPEN_CREATE | - GNUNET_DISK_OPEN_APPEND, - GNUNET_DISK_PERM_USER_READ | - GNUNET_DISK_PERM_USER_WRITE | - GNUNET_DISK_PERM_GROUP_READ); - if (NULL == fh) - { - LOG (GNUNET_ERROR_TYPE_ERROR, - "Opening file `%s' failed.\n", - name); - GNUNET_assert (0); - } - GNUNET_CONTAINER_multihashmap_put (open_files, - &hash, - fh, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); + if (NULL != (fh = GNUNET_CONTAINER_multihashmap_get (open_files, + &hash))) return fh; - } - else + fh = GNUNET_DISK_file_open (name, + GNUNET_DISK_OPEN_WRITE | + GNUNET_DISK_OPEN_CREATE | + GNUNET_DISK_OPEN_APPEND, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE | + GNUNET_DISK_PERM_GROUP_READ); + if (NULL == fh) { - fh = GNUNET_CONTAINER_multihashmap_get (open_files, - &hash); - return fh; + LOG (GNUNET_ERROR_TYPE_ERROR, + "Opening file `%s' failed.\n", + name); + GNUNET_assert (0); } + GNUNET_assert (GNUNET_YES == + GNUNET_CONTAINER_multihashmap_put (open_files, + &hash, + fh, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); + return fh; } -- 2.25.1