Skip to content

Commit

Permalink
GH-1076: Fix bro_srandom() to replace 0 seeds with 1
Browse files Browse the repository at this point in the history
The bro_prng() implementation cannot generate 0 as a result since it
causes every subsequent number from the PRNG to also be 0, so use the
number 1 instead of 0.
  • Loading branch information
jsiwek committed Jul 22, 2020
1 parent 2f15f5b commit 4a6622d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/util.cc
Expand Up @@ -1072,7 +1072,7 @@ static unsigned int first_seed = 0;

static void bro_srandom(unsigned int seed, bool deterministic)
{
bro_rand_state = seed;
bro_rand_state = seed == 0 ? 1 : seed;
bro_rand_determistic = deterministic;

srandom(seed);
Expand All @@ -1081,7 +1081,7 @@ static void bro_srandom(unsigned int seed, bool deterministic)
void bro_srandom(unsigned int seed)
{
if ( bro_rand_determistic )
bro_rand_state = seed;
bro_rand_state = seed == 0 ? 1 : seed;
else
srandom(seed);
}
Expand Down
6 changes: 6 additions & 0 deletions testing/btest/Baseline/bifs.rand/out.4
@@ -0,0 +1,6 @@
0
131
755
4
634
473
3 changes: 3 additions & 0 deletions testing/btest/bifs/rand.zeek
Expand Up @@ -2,9 +2,12 @@
# @TEST-EXEC: zeek -b %INPUT >out
# @TEST-EXEC: zeek -b %INPUT do_seed=F >out.2
# @TEST-EXEC: unset ZEEK_SEED_FILE && zeek -b %INPUT real_random=T >out.3
# @TEST-EXEC: for i in $(seq 21); do echo 0 >>random-zero.seed; done
# @TEST-EXEC: ZEEK_SEED_FILE=random-zero.seed zeek -b %INPUT >out.4
# @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff out.2
# @TEST-EXEC: btest-diff out.3
# @TEST-EXEC: btest-diff out.4

const do_seed = T &redef;
const real_random = F &redef;
Expand Down

0 comments on commit 4a6622d

Please sign in to comment.