diff --git a/base/openvas_hosts.c b/base/openvas_hosts.c index 13282dbaa..20d46f33a 100644 --- a/base/openvas_hosts.c +++ b/base/openvas_hosts.c @@ -1165,34 +1165,46 @@ openvas_hosts_free (openvas_hosts_t *hosts) void openvas_hosts_shuffle (openvas_hosts_t *hosts) { - int count; - GList *new_list; + size_t i = 0; GRand *rand; + GList *element; + void **shuffle_array; if (hosts == NULL) return; - count = openvas_hosts_count (hosts); - new_list = NULL; - + /* Store all host pointers in an array. */ + shuffle_array = g_malloc0 (hosts->count * sizeof (openvas_host_t *)); + element = hosts->hosts; + while (element) + { + shuffle_array[i] = element->data; + element = element->next; + i++; + } + /* Shuffle the array. */ rand = g_rand_new (); + for (i = 0; i < hosts->count; i++) + { + void *tmp; + int j = g_rand_int_range (rand, 0, hosts->count); - while (count) + tmp = shuffle_array[i]; + shuffle_array[i] = shuffle_array[j]; + shuffle_array[j] = tmp; + } + /* Insert shuffled hosts in the list. */ + element = hosts->hosts; + for (i = 0; i < hosts->count; i++) { - GList *element; - - /* Get element from random position [0, count[. */ - element = g_list_nth (hosts->hosts, g_rand_int_range (rand, 0, count)); - /* Remove it. */ - hosts->hosts = g_list_remove_link (hosts->hosts, element); - /* Insert it in new list */ - new_list = g_list_concat (element, new_list); - count--; + element->data = shuffle_array[i]; + element = element->next; } - hosts->hosts = new_list; - hosts->current = hosts->hosts; + hosts->current = hosts->hosts; g_rand_free (rand); + g_free (shuffle_array); + } /**