[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: mktemp segfaults



On Mon, Oct 18, 1999 at 07:14:03 +0000, Bernhard Rieder wrote:
> When calling mktemp I always get a segfault. This short program was used
> for testing.
> 
> #include <stdlib.h>
> 
> char *template = "/tmp/tmpfileXXXXXX";
> 
> int main () {
>    return printf("%s\n", mktemp(template));
> }

Quoting the fine manual `info libc "Temporary Files"':
:   *Note:* Because `mktemp' and `mkstemp' modify the template string,
:you _must not_ pass string constants to them.  String constants are
:normally in read-only storage, so your program would crash when `mktemp' or
:`mkstemp' tried to modify the string.

> $ gcc -o test test.c

As a workaround, you can compile -fwritable-strings. The proper solution is
to ensure that *template is writable; there's bound to be a clean way to do
that but I don't have one handy (except via malloc and strcpy).

HTH,
Ray
-- 
J.H.M. Dassen                 | RUMOUR  Believe all you hear. Your world may  
jdassen@wi.LeidenUniv.nl      | not be a better one than the one the blocks   
                              | live in but it'll be a sight more vivid.      
                              |     - The Hipcrime Vocab by Chad C. Mulligan  


Reply to: