Writeability of strings

Rich Salz rsalz at bbn.com
Tue Dec 20 02:24:17 AEST 1988


Simple question, fast answers...  In <1316 at papaya.bbn.com> I asked:

>Simple question.  Which is legal and/or "safer":
>	char *foo = "/tmp/XXXXXX";
>	(void)mktemp(foo);
Bad code.  "Anonymous" strings can be stuck in ROM.  May 13 draft, page 71
(I have it, I should'a looked there first.)

>	char *foo = mktemp("/tmp/XXXXXX");
Bad code for the same reason.

>	char foo[] = "/tmp/XXXXXX";
>	(void)mktemp(foo);
Okay code, but might cause problems for some mktemp() routines (see below).

>	#define TEMPNAME "/tmp/XXXXXX";
>	char foo[sizeof TEMPNAME];
>	(void)mktemp(strcpy(foo, TEMPNAME));
This is the best.  Some versions of mktemp() will fail if given an ending
other than XXXXXX.  (The ones I'm familiar with just blithely assume they
can clobber the last six characters, and don't check, but I've just been
"lucky" I guess.)  David Koblas knows about a buggy mktemp() that goes one
step beyond, and suggested changing the declaration of foo to be
	char foo [sizeof TEMPNAME + 2];
which is kinda icky.

Chris Torek warns about compilers that think -- wrongly -- think
sizeof("string")==sizeof(char *), when it should be strlen("string") + 1.

>Thanks!
Thanks, indeed, to at least the following:
	Jerry Schwarz, AT&T Bell Labs, <jss at ulysses.att.com>
	David Koblas, MIPS Computer Systems, <koblas at mips.com>
	Chris Torek, University of Maryland, <chris at mimsy.umd.edu>
	John Haugh, <jfh at rpp386.dallas.tx.us>
	Karl Heuer, Interactive Systems, <karl at haddock.isc.com>
	Colin, Microsoft Corp., <uunet!microsof!w-colinp>

/rich $alz
-- 
Please send comp.sources.unix-related mail to rsalz at uunet.uu.net.



More information about the Comp.std.c mailing list