mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
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.
This commit is contained in:
@@ -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 */
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user