From 04901bd0466c485354bef4bfc616a9049a40c115 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Thu, 11 Oct 2012 21:23:06 +0000 Subject: [PATCH] fixes for invalid reads --- src/include/gnunet_testbed_service.h | 3 ++- src/testbed/sample_hosts.txt | 10 ++++++++++ src/testbed/test_testbed_api_hosts.c | 3 ++- src/testbed/testbed_api_hosts.c | 23 ++++++++++++++--------- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/include/gnunet_testbed_service.h b/src/include/gnunet_testbed_service.h index 065cd0c0f..ba3b8961f 100644 --- a/src/include/gnunet_testbed_service.h +++ b/src/include/gnunet_testbed_service.h @@ -113,7 +113,8 @@ GNUNET_TESTBED_host_create_with_id (uint32_t id, * Load a set of hosts from a configuration file. * * @param filename file with the host specification - * @param hosts set to the hosts found in the file + * @param hosts set to the hosts found in the file; caller must free this if + * number of hosts returned is greater than 0 * @return number of hosts returned in 'hosts', 0 on error */ unsigned int diff --git a/src/testbed/sample_hosts.txt b/src/testbed/sample_hosts.txt index 61b07f3c2..5b661696b 100644 --- a/src/testbed/sample_hosts.txt +++ b/src/testbed/sample_hosts.txt @@ -3,3 +3,13 @@ totakura@192.168.0.2:22 totakura@192.168.0.3:22 totakura@192.168.0.4:22 totakura@192.168.0.5:22 +totakura@192.168.0.6:22 +totakura@192.168.0.7:22 +totakura@192.168.0.8:22 +totakura@192.168.0.9:22 +totakura@192.168.0.10:22 +totakura@192.168.0.11:22 +totakura@192.168.0.12:22 +totakura@192.168.0.13:22 +totakura@192.168.0.14:22 +totakura@192.168.0.15:22 diff --git a/src/testbed/test_testbed_api_hosts.c b/src/testbed/test_testbed_api_hosts.c index e98bc8e21..0102b9e26 100644 --- a/src/testbed/test_testbed_api_hosts.c +++ b/src/testbed/test_testbed_api_hosts.c @@ -73,6 +73,7 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_TESTBED_host_destroy (hosts[num_hosts - 1]); num_hosts--; } + GNUNET_free (hosts); } @@ -99,7 +100,7 @@ run (void *cls, char *const *args, const char *cfgfile, hosts = NULL; num_hosts = GNUNET_TESTBED_hosts_load_from_file ("sample_hosts.txt", &hosts); - GNUNET_assert (5 == num_hosts); + GNUNET_assert (15 == num_hosts); GNUNET_assert (NULL != hosts); shutdown_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (2), &do_shutdown, NULL); diff --git a/src/testbed/testbed_api_hosts.c b/src/testbed/testbed_api_hosts.c index 91fffc92d..4a69a17dd 100644 --- a/src/testbed/testbed_api_hosts.c +++ b/src/testbed/testbed_api_hosts.c @@ -288,7 +288,8 @@ GNUNET_TESTBED_host_create (const char *hostname, const char *username, * Load a set of hosts from a configuration file. * * @param filename file with the host specification - * @param hosts set to the hosts found in the file + * @param hosts set to the hosts found in the file; caller must free this if + * number of hosts returned is greater than 0 * @return number of hosts returned in 'hosts', 0 on error */ unsigned int @@ -339,6 +340,8 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename, if (((data[offset] == '\n')) && (buf != &data[offset])) { data[offset] = '\0'; + username = NULL; + hostname = NULL; ret = SSCANF (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd", &username, &hostname, &port); if (3 == ret) @@ -347,19 +350,19 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename, "Successfully read host %s, port %d and user %s from file\n", hostname, port, username); /* We store hosts in a static list; hence we only require the starting - host pointer in that list to get the newly created list of hosts */ + host pointer in that list to access the newly created list of hosts */ if (NULL == starting_host) starting_host = GNUNET_TESTBED_host_create (hostname, username, port); else (void) GNUNET_TESTBED_host_create (hostname, username, port); count++; - GNUNET_free (hostname); - GNUNET_free (username); } else GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Error reading line `%s' in hostfile\n", buf); + GNUNET_free_non_null (hostname); + GNUNET_free_non_null (username); buf = &data[offset + 1]; } else if ((data[offset] == '\n') || (data[offset] == '\0')) @@ -368,7 +371,10 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename, GNUNET_free (data); if (NULL == starting_host) return 0; - *hosts = &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)]; + *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count); + memcpy (*hosts, + &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)], + sizeof (struct GNUNET_TESTBED_Host *) * count); return count; } @@ -411,10 +417,9 @@ GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host) break; host_list_size -= HOST_LIST_GROW_STEP; } - if (orig_size != host_list_size) - host_list = - GNUNET_realloc (host_list, - sizeof (struct GNUNET_TESTBED_Host *) * host_list_size); + host_list = + GNUNET_realloc (host_list, + sizeof (struct GNUNET_TESTBED_Host *) * host_list_size); } -- 2.25.1