A better way to solve this I think. Rich.
Richard W.M. Jones
2017-Jan-26 19:31 UTC
[Libguestfs] [PATCH v2 1/2] common/edit: Don't use random() function.
Generate the random filename using our utility function guestfs_int_random_string. This also means that we will not need to call srandom() in guestfish or virt-edit. --- common/edit/file-edit.c | 18 ++++++------------ edit/edit.c | 3 --- fish/fish.c | 3 --- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/common/edit/file-edit.c b/common/edit/file-edit.c index a730011..81b1f1f 100644 --- a/common/edit/file-edit.c +++ b/common/edit/file-edit.c @@ -303,18 +303,10 @@ do_upload (guestfs_h *g, const char *fn, const char *tempfile, return 0; } -static char -random_char (void) -{ - const char c[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - return c[random () % (sizeof c - 1)]; -} - static char * generate_random_name (const char *filename) { char *ret, *p; - size_t i; ret = malloc (strlen (filename) + 16); if (!ret) { @@ -328,11 +320,13 @@ generate_random_name (const char *filename) p++; /* Because of "+ 16" above, there should be enough space in the - * output buffer to write 8 random characters here. + * output buffer to write 8 random characters here plus the + * trailing \0. */ - for (i = 0; i < 8; ++i) - *p++ = random_char (); - *p++ = '\0'; + if (guestfs_int_random_string (p, 8) == -1) { + perror ("guestfs_int_random_string"); + return NULL; + } return ret; /* caller will free */ } diff --git a/edit/edit.c b/edit/edit.c index 2f986a3..6807c28 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -102,9 +102,6 @@ main (int argc, char *argv[]) bindtextdomain (PACKAGE, LOCALEBASEDIR); textdomain (PACKAGE); - /* We use random(3) below. */ - srandom (time (NULL)); - enum { HELP_OPTION = CHAR_MAX + 1 }; static const char options[] = "a:b:c:d:e:m:vVx"; diff --git a/fish/fish.c b/fish/fish.c index b7d63cf..beccafd 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -180,9 +180,6 @@ main (int argc, char *argv[]) bindtextdomain (PACKAGE, LOCALEBASEDIR); textdomain (PACKAGE); - /* We use random(3) in edit.c. */ - srandom (time (NULL)); - parse_config (); enum { HELP_OPTION = CHAR_MAX + 1 }; -- 2.9.3
Richard W.M. Jones
2017-Jan-26 19:31 UTC
[Libguestfs] [PATCH v2 2/2] tests/mount-local, tests/parallel: Use srandom (time (NULL) + getpid ()).
If two instances of the test start at exactly the same time, they would have the same random number seed. --- tests/mount-local/test-parallel-mount-local.c | 2 +- tests/parallel/test-parallel.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/mount-local/test-parallel-mount-local.c b/tests/mount-local/test-parallel-mount-local.c index 3e5d95c..542f1ed 100644 --- a/tests/mount-local/test-parallel-mount-local.c +++ b/tests/mount-local/test-parallel-mount-local.c @@ -85,7 +85,7 @@ main (int argc, char *argv[]) int r, errors = 0; void *status; - srandom (time (NULL)); + srandom (time (NULL) + getpid ()); /* If the --test flag is given, then this is the test subprocess. */ if (argc == 3 && STREQ (argv[1], "--test")) { diff --git a/tests/parallel/test-parallel.c b/tests/parallel/test-parallel.c index 20100d1..276340e 100644 --- a/tests/parallel/test-parallel.c +++ b/tests/parallel/test-parallel.c @@ -31,6 +31,7 @@ #include <signal.h> #include <errno.h> #include <error.h> +#include <sys/types.h> #include <pthread.h> @@ -74,7 +75,7 @@ main (int argc, char *argv[]) size_t i, errors = 0; void *status; - srandom (time (NULL)); + srandom (time (NULL) + getpid ()); /* Only run this test when invoked by check-slow. */ slow = getenv ("SLOW"); -- 2.9.3