diff options
author | David Robillard <d@drobilla.net> | 2023-02-04 19:21:25 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-02-04 19:21:25 -0500 |
commit | 0e1b7957b209a5885b6d3eca9930a899be3af80d (patch) | |
tree | eba49ba940d0718cdc048d76e6a1b39fb35d14e8 /src | |
parent | 5fa282a1a0c0ebec5704f223be301892fba9bcbe (diff) | |
download | zix-0e1b7957b209a5885b6d3eca9930a899be3af80d.tar.gz zix-0e1b7957b209a5885b6d3eca9930a899be3af80d.tar.bz2 zix-0e1b7957b209a5885b6d3eca9930a899be3af80d.zip |
Avoid use of rand()
Diffstat (limited to 'src')
-rw-r--r-- | src/win32/filesystem_win32.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/win32/filesystem_win32.c b/src/win32/filesystem_win32.c index 5c787c9..0484684 100644 --- a/src/win32/filesystem_win32.c +++ b/src/win32/filesystem_win32.c @@ -71,6 +71,16 @@ zix_copy_file(ZixAllocator* const allocator, CopyFile(src, dst, !(options & ZIX_COPY_OPTION_OVERWRITE_EXISTING))); } +/// Linear Congruential Generator for making random 32-bit integers +static inline uint32_t +lcg32(const uint32_t i) +{ + static const uint32_t a = 134775813U; + static const uint32_t c = 1U; + + return (a * i) + c; +} + char* zix_create_temporary_directory(ZixAllocator* const allocator, const char* const path_pattern) @@ -94,9 +104,11 @@ zix_create_temporary_directory(ZixAllocator* const allocator, // Repeatedly try creating a directory with random suffixes memcpy(result, path_pattern, length + 1U); char* const suffix = result + length - 6U; + uint32_t seed = GetCurrentProcessId(); for (unsigned attempt = 0U; attempt < 128U; ++attempt) { for (unsigned i = 0U; i < 6U; ++i) { - suffix[i] = chars[rand() % n_chars]; + seed = lcg32(seed); + suffix[i] = chars[seed % n_chars]; } if (!_mkdir(result)) { |