#13660: mktemp() generates completely deterministic filenames
---------------------------------+----------------------------
Reporter: david.given | Owner: phoudoin
Type: bug | Status: closed
Priority: normal | Milestone: Unscheduled
Component: System/libroot.so | Version: R1/Development
Resolution: fixed | Keywords:
Blocked By: | Blocking:
Has a Patch: 0 | Platform: All
---------------------------------+----------------------------
Comment (by david.given):
Sorry --- that's not going to work!
- by calling `srand()` you're overwriting any seed used by the user, so
disturbing any random number sequences they've set up.
- `gettimeofday()` is not guaranteed to return unique values. If the user
calls `mktemp()` twice in very short succession, with an interval smaller
than the timer granularity, then you end up seeding the random number
generator twice with the same value, which means the two calls to
`mktemp()` will generate the same filename.
I'd do something like this:
{{{
static int seed = 0; // value maintained between calls
if (seed == 0) {
// seed the PRNG once on first call
struct timeval tv;
gettimeofday(&tv, 0);
seed = (getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec;
}
...
uint32_t value = rand_r(&seed) % (sizeof(padchar) - 1);
}}}
--
Ticket URL: <https://dev.haiku-os.org/ticket/13660#comment:5>
Haiku <https://dev.haiku-os.org>
The Haiku operating system.