* fix logic for srand() I got reversed

* introduce HAVE_ARC4RANDOM tristate (0/1/undef) and fill it appropriately
* NetBSD(R) joins the list of OSes which don't have arc4random
This commit is contained in:
tg 2005-05-26 23:01:30 +00:00
parent 7f4297f3d0
commit dc22f4da3a
3 changed files with 16 additions and 12 deletions

6
main.c
View File

@ -1,4 +1,4 @@
/** $MirOS: src/bin/mksh/main.c,v 1.7 2005/05/25 23:31:07 tg Exp $ */ /** $MirOS: src/bin/mksh/main.c,v 1.8 2005/05/26 23:01:30 tg Exp $ */
/* $OpenBSD: main.c,v 1.38 2005/03/30 17:16:37 deraadt Exp $ */ /* $OpenBSD: main.c,v 1.38 2005/03/30 17:16:37 deraadt Exp $ */
/* $OpenBSD: tty.c,v 1.8 2005/03/30 17:16:37 deraadt Exp $ */ /* $OpenBSD: tty.c,v 1.8 2005/03/30 17:16:37 deraadt Exp $ */
/* $OpenBSD: io.c,v 1.21 2005/03/30 17:16:37 deraadt Exp $ */ /* $OpenBSD: io.c,v 1.21 2005/03/30 17:16:37 deraadt Exp $ */
@ -10,7 +10,7 @@
#include <ctype.h> #include <ctype.h>
#include <pwd.h> #include <pwd.h>
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.7 2005/05/25 23:31:07 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/main.c,v 1.8 2005/05/26 23:01:30 tg Exp $");
extern char **environ; extern char **environ;
@ -200,7 +200,7 @@ main(int argc, char *argv[])
setstr(pwd_v, current_wd, KSH_RETURN_ERROR); setstr(pwd_v, current_wd, KSH_RETURN_ERROR);
} }
ppid = getppid(); ppid = getppid();
#if !defined(__gnu_linux__) && !defined(__INTERIX) && !defined(__sun__) #if !HAVE_ARC4RANDOM
srand((*((long *)kshname)) ^ ((long)time(NULL) * kshpid * ppid)); srand((*((long *)kshname)) ^ ((long)time(NULL) * kshpid * ppid));
#endif #endif
setint(global("PPID"), (long) ppid); setint(global("PPID"), (long) ppid);

10
sh.h
View File

@ -1,4 +1,4 @@
/** $MirOS: src/bin/mksh/sh.h,v 1.8 2005/05/25 23:31:08 tg Exp $ */ /** $MirOS: src/bin/mksh/sh.h,v 1.9 2005/05/26 23:01:30 tg Exp $ */
/* $OpenBSD: sh.h,v 1.27 2005/03/28 21:33:04 deraadt Exp $ */ /* $OpenBSD: sh.h,v 1.27 2005/03/28 21:33:04 deraadt Exp $ */
/* $OpenBSD: shf.h,v 1.5 2005/03/30 17:16:37 deraadt Exp $ */ /* $OpenBSD: shf.h,v 1.5 2005/03/30 17:16:37 deraadt Exp $ */
/* $OpenBSD: table.h,v 1.6 2004/12/18 20:55:52 millert Exp $ */ /* $OpenBSD: table.h,v 1.6 2004/12/18 20:55:52 millert Exp $ */
@ -1340,4 +1340,12 @@ size_t confstr(int, char *, size_t);
} while (0) } while (0)
#endif #endif
#ifndef HAVE_ARC4RANDOM
#if defined(__gnu_linux__) || defined(__INTERIX) || defined(__sun__) \
|| defined(__NetBSD__)
#define HAVE_ARC4RANDOM 0
#else
#define HAVE_ARC4RANDOM 1
#endif
#endif #endif

12
var.c
View File

@ -1,4 +1,4 @@
/** $MirOS: src/bin/mksh/var.c,v 1.4 2005/05/25 13:46:02 tg Exp $ */ /** $MirOS: src/bin/mksh/var.c,v 1.5 2005/05/26 23:01:30 tg Exp $ */
/* $OpenBSD: var.c,v 1.26 2005/03/30 17:16:37 deraadt Exp $ */ /* $OpenBSD: var.c,v 1.26 2005/03/30 17:16:37 deraadt Exp $ */
#include "sh.h" #include "sh.h"
@ -6,7 +6,7 @@
#include <ctype.h> #include <ctype.h>
#include <time.h> #include <time.h>
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.4 2005/05/25 13:46:02 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/var.c,v 1.5 2005/05/26 23:01:30 tg Exp $");
/* /*
* Variables * Variables
@ -855,11 +855,7 @@ makenv(void)
* Someone has set the srand() value, therefore from now on * Someone has set the srand() value, therefore from now on
* we return values from rand() instead of arc4random() * we return values from rand() instead of arc4random()
*/ */
#if !defined(__gnu_linux__) && !defined(__INTERIX) && !defined(__sun__) int use_rand = !HAVE_ARC4RANDOM;
int use_rand = 0;
#else
int use_rand = 1;
#endif
/* /*
* Called after a fork in parent to bump the random number generator. * Called after a fork in parent to bump the random number generator.
@ -919,7 +915,7 @@ getspec(struct tbl *vp)
vp->flag &= ~SPECIAL; vp->flag &= ~SPECIAL;
if (use_rand) if (use_rand)
setint(vp, (long) (rand() & 0x7fff)); setint(vp, (long) (rand() & 0x7fff));
#if !defined(__gnu_linux__) && !defined(__INTERIX) && !defined(__sun__) #if HAVE_ARC4RANDOM
else else
setint(vp, (long) (arc4random() & 0x7fff)); setint(vp, (long) (arc4random() & 0x7fff));
#endif #endif