Skip to content

Commit

Permalink
Remove undefined behavior from sranddev() and
Browse files Browse the repository at this point in the history
srandomdev(). This doesn't actually work
with any modern C compiler:

In particular, both clang and modern gcc
verisons silently elide any xor operation
with 'junk'.

Approved by:	secteam
MFC after:	3 days
  • Loading branch information
grimreaper committed Oct 9, 2012
1 parent af2bdac commit 6a762eb
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/libc/stdlib/rand.c
Expand Up @@ -130,10 +130,9 @@ sranddev()

if (!done) {
struct timeval tv;
unsigned long junk;

gettimeofday(&tv, NULL);
srand((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk);
srand((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec);
}
}

Expand Down
3 changes: 1 addition & 2 deletions lib/libc/stdlib/random.c
Expand Up @@ -312,10 +312,9 @@ srandomdev(void)

if (!done) {
struct timeval tv;
volatile unsigned long junk;

gettimeofday(&tv, NULL);
srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk);
srandom((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec);
return;
}

Expand Down

0 comments on commit 6a762eb

Please sign in to comment.