c - lcc tmpnam crash -
when use lcc
compiler , call tmpnam(buf)
program crashes.
reason: l_tmpnam indicates buf must 14 bytes long, while string returned "d:\documents , settings\paul\temporary\tmp9.tmp" longer 14.
what wrong, how can behavior explained.
verbatim man tmpnam
:
never use function. use mkstemp(3) or tmpfile(3) instead.
anyway, asked it:
the name generated tmpnam()
consists of file name of maximum l_tmpnam
lenght prefixed directory of name p_tmpdir
.
so buffer passed tmpnam()
best declared (if c99):
char pathname[strlen(p_tmpdir) + 1 + l_tmpnam + 1] = ""; /* +1 dir delimiting `/` , +1 zero-termination */
if non c99 might go this:
size_t sizetmpname = strlen(p_tmpdir) + 1 + l_tmpnam + 1; char * pathname = calloc(sizetmpname, sizeof (*pathname)); if (null == pathname) perror("calloc() 'pathname'");
then call tmpnam()
this:
if (null == tmpnam(pathname)) fprintf(stderr, "tmpnam(): unique name cannot generated.\n"); else printf("unique name: %s\n", pathname); ... /* soemthing */ /* if on non c99 , calloc(() called: */ free(pathname);
Comments
Post a Comment