*BSD News Article 90589


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!news.rmit.EDU.AU!goanna.cs.rmit.edu.au!news.apana.org.au!cantor.edge.net.au!news.teragen.com.au!news.access.net.au!news.mel.connect.com.au!munnari.OZ.AU!uunet!in3.uu.net!208.206.176.15!dimensional.com!news.wizard.com !news.ici.net!not-for-mail
From: Alicia Carla Longstreet <carla@ici.net>
Newsgroups: comp.lang.c,comp.unix.bsd.freebsd.misc
Subject: Re: What does gets() unsafe question mean?
Date: Thu, 06 Mar 1997 23:22:39 -0500
Organization: The Computer Solution
Lines: 38
Message-ID: <331F980F.7767@ici.net>
References: <01bc2a8f$67037120$db083ccc@default>
Reply-To: carla@ici.net
NNTP-Posting-Host: d-ma-fallriver-68.ici.net
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 3.01Gold (Win95; I)
To: "Jeffrey M. Metcalf" <metcalf@snet.net>
Xref: euryale.cc.adfa.oz.au comp.lang.c:195636 comp.unix.bsd.freebsd.misc:36731

Jeffrey M. Metcalf wrote:
> 
> Hello,
 
> I recently wrote a little C program which uses the stdio.h function gets().
> I compiled and ran it under FreeBSD and I get..
 
> warning: this program uses gets(), which is unsafe.
 
> What exactly does this mean?  Why is it unsafe?  If possible, can any
> replies be sent as an e-mail to the above address?

I can't be sure why your compiler is issueing a warning, it might be due
to an implementation specific requirement. Or it might be a very nice
compiler.

My reference on Standard C says:

	The number of characters that gets() reads and stores cannot be
limited.

This would be a very good reason not to use gets().  I would reccomend
using fgets() with stdin for the stream.

char *fgets( char *s, int n, FILE *stream )

This will limit input to n-1 characters.

-- 
********************************************
* Alicia Carla Longstreet     carla@ici.net
********************************************

Knowledge is free..., 
but you do have to pay me for my time and effort 
in presenting the knowledge in a manner that 
makes it easier for you to aquire.
You are free to reinvent the wheel anytime you please.