v05i053: A "safe" replacement for gets()

randy orrison randy at halcdc.UUCP
Sat Nov 19 04:48:49 AEST 1988


In article <468 at auspex.UUCP> guy at auspex.UUCP (Guy Harris) writes:
|>|#define gets(s) fgets(s, sizeof s, stdin)
|...
|>	gets (s);
|
|>Two problems: the space after gets would kill it if it's a macro,
|
|Say WHAT?  Works fine on all the compilers I know of.  "Preprocessor"
|macros don't require that you give the exact same amount of white space
|in the invocation as you gave in the definition; if your compiler
|requires this, get it fixed!

Obviously correct.  (Sorry Brandon!) What I was thinking of was the
time i tried something along the lines of:
#define gets (s) fgets(s, sizeof s, stdin)
	    ^
which fails for obvious reasons.

However, it still doesn't work for cases where s is a pointer instead
of an array - say, if it's a parameter in a function, or declared as a
char * with memory allocated for it.  It's not something you could
stick in at the top of someone else's code to make it secure, and it's
not something you'd want to use in original code (which you could do
right).

	-randy
-- 
Randy Orrison - Control Data in the Hills of Arden	randy at halcdc.uucp
aka randy@{ux.acss.umn.edu, umnacvx.bitnet, cctb.mn.org, umn-cs.uucp}

ZQOTD:	"Yow!  It's a hole all the way to downtown Burbank!"



More information about the Comp.lang.c mailing list