Mail Archives: djgpp/2000/05/06/21:09:35
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 fgets() 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.
What was I drinking when I wrote this?
>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 15 (+1) characters in bar
because foo can never have more than 15 (+1) 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 that fscanf() suffers from the
same disease as gets().
--
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 -