Skip to content

Commit 712f6cf

Browse files
pks-tgitster
authored andcommitted
reftable/system: introduce reftable_rand()
Introduce a new system-level `reftable_rand()` function that generates a single unsigned integer for us. The implementation of this function is to be provided by the calling codebase, which allows us to more easily hook into pre-seeded random number generators. Adapt the two callsites where we generated random data. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 01a587d commit 712f6cf

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

reftable/stack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
523523
close(fd);
524524
fd = -1;
525525

526-
delay = delay + (delay * git_rand(CSPRNG_BYTES_INSECURE)) / UINT32_MAX + 1;
526+
delay = delay + (delay * reftable_rand()) / UINT32_MAX + 1;
527527
sleep_millisec(delay);
528528
}
529529

@@ -688,7 +688,7 @@ int reftable_stack_add(struct reftable_stack *st,
688688
static int format_name(struct reftable_buf *dest, uint64_t min, uint64_t max)
689689
{
690690
char buf[100];
691-
uint32_t rnd = git_rand(CSPRNG_BYTES_INSECURE);
691+
uint32_t rnd = reftable_rand();
692692
snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x",
693693
min, max, rnd);
694694
reftable_buf_reset(dest);

reftable/system.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
#include "../lockfile.h"
55
#include "../tempfile.h"
66

7+
uint32_t reftable_rand(void)
8+
{
9+
return git_rand(CSPRNG_BYTES_INSECURE);
10+
}
11+
712
int tmpfile_from_pattern(struct reftable_tmpfile *out, const char *pattern)
813
{
914
struct tempfile *tempfile;

reftable/system.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ license that can be found in the LICENSE file or at
1414
#include "git-compat-util.h"
1515
#include "compat/zlib-compat.h"
1616

17+
/*
18+
* Return a random 32 bit integer. This function is expected to return
19+
* pre-seeded data.
20+
*/
21+
uint32_t reftable_rand(void);
22+
1723
/*
1824
* An implementation-specific temporary file. By making this specific to the
1925
* implementation it becomes possible to tie temporary files into any kind of

0 commit comments

Comments
 (0)