delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/06/20:50:11

From: Damian Yerrick <Bullcr_pd_yerrick AT hotmail DOT comRemoveBullcr_p>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: reading text files
Organization: Pin Eight Software http://pineight.8m.com/
Message-ID: <ikc9hsc1q0d54vjj15rlgj5f6bkfl198lq@4ax.com>
References: <s0ptgsk9052i4lft8hm38o3r0ugjncb64e AT 4ax DOT com> <Pine DOT SUN DOT 3 DOT 91 DOT 1000503115802 DOT 4851L-100000 AT is> <l476hs8q4tskk01uq232gj9f30scupjea5 AT 4ax DOT com> <200005062141 DOT RAA07172 AT indy DOT delorie DOT com>
X-Newsreader: Forte Agent 1.7/32.534
MIME-Version: 1.0
Lines: 70
X-Trace: +SWaIpjV1/yWxvTagw+pRTDOQo0aJFJIivdgJTvMUO6Pi3jUWKK/Iqcr9XR/RiJTQ/qbctjutls6!otsVu73z9QQmL9O9beYBE7pL4hanSv+SvKedttKXl7T3xaLiAUPmG7piqDCAMciZTl1rRjTJaK7V!wN/dV2c=
X-Complaints-To: abuse AT gte DOT net
X-Abuse-Info: Please be sure to forward a copy of ALL headers
X-Abuse-Info: Otherwise we will be unable to process your complaint properly
NNTP-Posting-Date: Sun, 07 May 2000 00:12:44 GMT
Distribution: world
Date: Sun, 07 May 2000 00:12:44 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sat, 6 May 2000 17:41:43 -0400 (EDT), Eli Zaretskii
<eliz AT delorie DOT com> wrote:

>Damian Yerrick wrote:
>
>> int main(void)
>> {
>>   char foo[16];
>>   FILE *fp = fopen("foo.txt", "rb");
>> 
>>   if(!fp)
>>   {
>>     puts("couldn't open foo.txt for writing.\n"
>>          "It should contain one line with one very long word.");
>>     return 1;
>>   }
>>   fscanf(fp, "%s", foo);
>>   printf("read word: %s\n", foo);
>>   return 0;
>
>If you replace fscanf with fgets+sscanf in this case, without changing
>the format or anything else, it will blow up the stack in exactly the
>same way.  Did you try it?

But if you tell fscanf to read only n characters, where n is small
enough to fit comfortably into your buffer, everything will be nice.

>> sscanf() knows that no incoming string will be longer than the input
>> string.
>
>Sorry, I cannot parse this statement.  Care to explain?

char foo[16];
char bar[16];

fgets(foo, 16, fp);
sscanf(foo, "%s", bar);

This will never return more than 16 characters in bar
because foo can never have more than 16 characters.

>Also, the reason for the crash in the program you posted is that
>the buffer foo[] is too small to accept the input from the file.

Exactly my point. Using fscanf() can result in buffer overflows
because there is no maximum size for the output buffers.

>`sscanf' cannot solve this problem, since it doesn't know how large
>is its third argument.  It only knows how large is its first argument.

Yes, but the other arguments can be made large enough to hold the
largest thing that sscanf() can put into them; they're never larger
than the first argument, and if the first argument has a known maximum
size (thanks to fgets()), sscanf() cannot overflow the buffer.

>Finally, there should be no reason to use `fscanf' to read a string
>with "%s" format.  `fscanf' is for converting text into non-text data,
>and when used as such, `sscanf' and `fscanf' behave even closer
>(i.e. blow or not in the same way).

It was an illustrative example to show some of the dangers of
fscanf().

-- 
Damian Yerrick
"I refuse to listen to those who refuse to listen to reason."
See the whole sig: http://www.rose-hulman.edu/~yerricde/sig.html

This is McAfee VirusScan. Add these two lines to your signature to
prevent the spread of signature viruses.  http://www.mcafee.com/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019