Skip to content

Commit 9abda98

Browse files
pks-tgitster
authored andcommitted
reftable/stack: fix use of unseeded randomness
When writing a new reftable stack, Git will first create the stack with a random suffix so that concurrent updates will not try to write to the same file. This random suffix is computed via a call to rand(3P). But we never seed the function via srand(3P), which means that the suffix is in fact always the same. Fix this bug by using `git_rand()` instead, which does not need to be initialized. While this function is likely going to be slower depending on the platform, this slowness should not matter in practice as we only use it when writing a new reftable stack. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3054fbd commit 9abda98

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

reftable/readwrite_test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ static void test_log_buffer_size(void)
141141
*/
142142
uint8_t hash1[GIT_SHA1_RAWSZ], hash2[GIT_SHA1_RAWSZ];
143143
for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
144-
hash1[i] = (uint8_t)(rand() % 256);
145-
hash2[i] = (uint8_t)(rand() % 256);
144+
hash1[i] = (uint8_t)(git_rand() % 256);
145+
hash2[i] = (uint8_t)(git_rand() % 256);
146146
}
147147
log.value.update.old_hash = hash1;
148148
log.value.update.new_hash = hash2;
@@ -320,7 +320,7 @@ static void test_log_zlib_corruption(void)
320320
};
321321

322322
for (i = 0; i < sizeof(message) - 1; i++)
323-
message[i] = (uint8_t)(rand() % 64 + ' ');
323+
message[i] = (uint8_t)(git_rand() % 64 + ' ');
324324

325325
reftable_writer_set_limits(w, 1, 1);
326326

reftable/stack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ int reftable_stack_add(struct reftable_stack *st,
434434
static void format_name(struct strbuf *dest, uint64_t min, uint64_t max)
435435
{
436436
char buf[100];
437-
uint32_t rnd = (uint32_t)rand();
437+
uint32_t rnd = (uint32_t)git_rand();
438438
snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x",
439439
min, max, rnd);
440440
strbuf_reset(dest);

0 commit comments

Comments
 (0)