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

Re: mktemp segfaults



On Mon, Oct 18, 1999 at 11:46:47AM +0200, J.H.M. Dassen Ray" wrote:
> 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).

Would...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	char *dir = "/tmp", *prefix = "fivec", *fname;
	if (NULL == (fname = tempnam(dir,prefix))) {
		fprintf(stderr,"Unable to make temp file name.\n");
		exit(EXIT_FAILURE);
	}
	printf("%s\n",fname);
	
	return 0;
}

work for ya?
-- 
+----------------------------------------------------+
| Eric G. Miller                        egm2@jps.net |
| GnuPG public key: http://www.jps.net/egm2/gpg.asc  |
+----------------------------------------------------+


Reply to: