libroot: mktemp.c: use rand_r() and set seed only on first call

This avoid to break caller's own srand() and having the same seed
if called twice very quickly.
Thanks david.given for a better fix for #13660.
This commit is contained in:
Philippe Houdoin 2017-08-10 10:26:14 +00:00
parent f7052a54cc
commit 22b7e29bc6

View File

@ -97,7 +97,7 @@ _gettemp(char *path, int *doopen, int domkdir, int slen)
char *pad;
struct stat sbuf;
int rval;
struct timeval tv;
static int seed = 0;
if (doopen != NULL && domkdir) {
__set_errno(EINVAL);
@ -116,11 +116,15 @@ _gettemp(char *path, int *doopen, int domkdir, int slen)
}
/* Fill space with random characters */
/* seed rand() first, otherwise generated sequences are same */
gettimeofday(&tv, 0);
srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
if (seed == 0) {
/* Select a pseudo-random seed on first call to avoid
to generate the same sequence of pattern */
struct timeval tv;
gettimeofday(&tv, 0);
seed = (getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec;
}
while (trv >= path && *trv == 'X') {
uint32_t value = rand() % (sizeof(padchar) - 1);
uint32_t value = rand_r(&seed) % (sizeof(padchar) - 1);
*trv-- = padchar[value];
}
start = trv + 1;