Daniel Lowe writes:
> Richard M Kreuter wrote:
> > What problem are you trying to solve?
> >From mktemp's manual page:
> Never use mktemp(). Some implementations follow 4.3BSD and replace
> XXXXXX by the current process ID and a single letter, so that at most
> 26 different names can be returned. Since on the one hand the names
> are easy to guess, and on the other hand there is a race between test‐
> ing whether the name exists and opening the file, every use of mktemp()
> is a security risk. The race is avoided by mkstemp(3).
This is all true, but the routine only uses mktemp() to generate a
random filename. Once it has a filename, it open()s with
O_CREAT|O_EXCL, and tries again with a new random filename in case open
fails with EEXIST. So while we might run out of random filenames on a
system that follows 4.3BSD as described, we're not subject to race
conditions between generating the name and creating the file. (However,
it seems that mktemp() is also not reentrant on some platforms, which is
a more serious problem.)
This is all academic, as the whole routine can be done more usefully in
Lisp. (When I wrote the routine I thought that using standard library
routines there had some benefit, but I was wrong.) I'll redo it in the
next couple days.