Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using fixed-seed prng #29

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/tgen-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ static gint _tgenmain_returnError(gint flushLogCache) {
}

static gint _tgenmain_run(gint argc, gchar *argv[]) {
/* We generally use glib's random number generation, but in the past subtle
* bugs have snuck in from using libc's random number generation without
* seeding it. Defensively seed it here. (glib's is always seeded from an
* appropriate source of entropy) */
srand(g_random_int());

/* get our hostname for logging */
gchar hostname[128] = {0};
tgenconfig_gethostname(hostname, 128);
Expand Down
2 changes: 1 addition & 1 deletion src/tgen-pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ void tgenpool_add(TGenPool* pool, gpointer item) {
gpointer tgenpool_getRandom(TGenPool* pool) {
TGEN_ASSERT(pool);
gint numNodes = g_tree_nnodes(pool->items);
gint position = (numNodes > 1) ? ((gint)(rand() % numNodes)) : 0;
gint position = (numNodes > 1) ? ((gint)(g_random_int() % numNodes)) : 0;
return g_tree_lookup(pool->items, &position);
}
4 changes: 2 additions & 2 deletions src/tgen-stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -1097,8 +1097,8 @@ static void _tgenstream_onReadable(TGenStream* stream) {
}

static GString* _tgenstream_getRandomString(gsize size) {
/* call rand() once to limit overhead */
gint r = rand() % 26;
/* call once to limit overhead */
gint r = g_random_int() % 26;
gchar c = (gchar)('a' + r);
/* fill the buffer. this was more efficient than malloc/memset and then g_string_new */
GString* buffer = g_string_new_len(NULL, (gssize)size);
Expand Down