[libc-commits] [libc] [libc] Add tmpnam implementation (PR #204901)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Sat Aug 1 00:24:30 PDT 2026


================
@@ -54,5 +54,92 @@ extern FILE *stderr;
 #ifndef SEEK_END
 #define SEEK_END 2
 #endif
+/*
----------------
kaladron wrote:

I'm sorry - you gave a great amount of detail (and thank you, I did request it!), but it's a bit too much for a system header.  This gives enough information for someone to reconstruct the number:

```c++
/*
 * L_tmpnam = 20 ("/tmp/" + 14 random chars + '\0').
 * Suffix length 14 (base-64) ensures a < 10^-12 collision
 * probability for up to 10^6 calls (via birthday bound).
 */
```

(Assuming you take the suggestion below to do base-64 instead of base-65)

Because it's Saturday morning, I asked Gemini to verify this for me:

To maintain your design constraints (a maximum of $10^6$ calls with a collision probability under $10^{-12}$), the birthday bound requires a minimum keyspace ($M$) of $5 \times 10^{23}$.

If we switch to a 64-character alphabet, we calculate the keyspace for $N$ characters:
 * $64^{13} \approx 3.02 \times 10^{23}$ (Falls slightly short of the target)
 * $64^{14} \approx 1.93 \times 10^{25}$ (Comfortably exceeds the target)

Because 13 characters isn't quite enough to guarantee your safety margin, you still have to round up to 14 characters.

The math results in a slightly smaller overall keyspace than base-65 ($1.93 \times 10^{25}$ vs $2.41 \times 10^{25}$), but it still easily clears the threshold.


https://github.com/llvm/llvm-project/pull/204901


More information about the libc-commits mailing list