retiring gets(3)

Dave Hammond daveh at marob.MASA.COM
Sat Nov 12 01:24:03 AEST 1988


In article <32301 at oliveb.olivetti.com> chase at Ozona.UUCP (David Chase) writes:
>You should also consider retiring certain features of 'scanf' and
>'fscanf'.  A call along the lines of
>
>    scanf("%s", junk);
>
>is perfectly able to scribble past the end of 'junk'.  I'm not sure if
>there are other holes like this built in to the standard i/o library;
>it wouldn't hurt to check.  (I've never been a real fan of 'scanf',
>but it does seem marginally more useful and harder to replace than
>'gets').

Carrying this line of thought foreward, it would seem that Mr. Chase
advocates retiring any library call which requires that the programmer
take responsibility for providing enough buffer space to handle the
data resulting from the call in question.

IMHO, if the programmer is aware that the library call does not know
about buffer length (which is obvious when no length parameter is passed
to the call), then it is the programmers responsibility to ensure that
(a) the buffer is of an appropriate length for his/her application, or
(b) if an appropriate length can not be determined, the call should *not*
be used.

[the unnecessary overhead of calling scanf("%s") instead of fgets()
 or a getc() loop might also be pointed out -- but I suspect the example
 was nothing more than that]

Dave Hammond
  UUCP: ...!uunet!masa.com!{marob,dsix2}!daveh
DOMAIN: daveh at marob.masa.com
----------------------------------------------------------------------------



More information about the Comp.lang.c mailing list