Skip to content

Commit

Permalink
Use thread-safe PRNG
Browse files Browse the repository at this point in the history
rand() isn't thread-safe on all platforms (musl libc for example)
use rand_r() instead
  • Loading branch information
job committed Jun 25, 2024
1 parent 7b72fe8 commit d5e3074
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/object/manifest.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,18 @@ build_rpp(struct Manifest *mft, struct rpki_uri *notif,
struct FileAndHash *fah, *tmpfah;
struct rpki_uri *uri;
int error;
unsigned int rnd;

rnd = time(NULL) ^ getpid();

*pp = rpp_create();

tal = tal_get_file_name(validation_tal(state_retrieve()));

/* Fisher-Yates shuffle with modulo bias */
srand(time(NULL) ^ getpid());
for (i = 0; i < mft->fileList.list.count - 1; i++) {
j = i + rand() % (mft->fileList.list.count - i);
rnd = rand_r(&rnd);
j = i + rnd % (mft->fileList.list.count - i);
tmpfah = mft->fileList.list.array[j];
mft->fileList.list.array[j] = mft->fileList.list.array[i];
mft->fileList.list.array[i] = tmpfah;
Expand Down

0 comments on commit d5e3074

Please sign in to comment.