The function TclpCreateTempFile() in unix/tclUnixPipe.c makes a number of dangerous assumptions that could potentially lead to array bounds violations, or at least portability issues.
The main problem is that an array fileName is declared with length L_tmpnam+9. This array is then filled with P_tmpdir, possibly a slash, and the string "tclXXXXXX". The code is written as if L_tmpnam is the length of P_tmpdir, but these two macros are entirely unrelated: L_tmpnam is the length of temporary filenames generated by the function tmpnam, and is unrelated to P_tmpdir. Thus, L_tmpnam could potentially be shorter than P_tmpdir (although I admit this is unlikely).
Also, if L_tmpnam is supposed to represent the length of P_tmpdir, the 9 extra bytes are insufficient: the string "tclXXXXXX" is 9 characters long, but room for the possible '/' and a '\0' are also needed.
A second assumption is that P_tmpdir is available in the first place.As far as I can tell P_tmpdir is not prescribed by any POSIX or C language standard, so blindly assuming that it is available, as tcl does, limits portability.
One easy solution to all problems is to do something like
#ifndef L_tmpdir
#define L_tmpdir "/tmp"
#endif
char fileName[] = L_tmpdir "/tclXXXXXX";
With the only flaw that an L_tmpdir ending with a '/' would result in filenames with '//' in them, which might confuse some systems.
(FYI, I came across this while porting tcl 8.5.8 to Minix.)
|