473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

getting a warning about gets

I'm getting a warning on my linux gcc compiler:

/tmp/ccXgHa9s.o(.text+0x48): In function `main':
: warning: the `gets' function is dangerous and should not be used.

And here's where I used gets:

#include "common.h" //common.h includes string.h
....
....

int main(void)
{
char zf_name[50];

....
printf("Enter the name of the file\n");
gets(zf_name);
...
return 0;
}
Jun 27 '08 #1
16 2177
On May 19, 10:38*am, pereges <Brol...@gmail.comwrote:
I'm getting a warning on my linux gcc compiler:

/tmp/ccXgHa9s.o(.text+0x48): In function `main':
: warning: the `gets' function is dangerous and should not be used.

And here's where I used gets:

#include "common.h" *//common.h includes string.h
...
...

int main(void)
{
* *char zf_name[50];

* *....
* *printf("Enter the name of the file\n");
* *gets(zf_name);
* *...
* *return 0;

}- Hide quoted text -

- Show quoted text -
I think the message is rather explicit; gets() is dangerous, and
shouldn't be used.

Alternatively, use fgets() or perhaps, scanf() to get input

--
include
Jun 27 '08 #2
Hi

On May 19, 11:38 am, pereges <Brol...@gmail.comwrote:
: warning: the `gets' function is dangerous and should not be used.
char zf_name[50];
gets(zf_name);

How do you know the file name is less than 50 characters?

Use:
char zf_name[ SOME_CONSTANT ];
fgets( zf_name, SOME_CONSTANT, stdin );

This will at least not overflow your buffer.

You should also do:

if( ferror( stdin )) /* there was a read error */

if( zf_name[ strlen(zf_name) - 1 ] == '\n' )
zf_name[ strlen(zf_name) - 1 ] = 0; /* take the newline character
off the filename */

else
/* line was longer than SOME_CONSTANT, and part of it is missing */
and maybe also:

if( feof( stdin )) /* end of file was reached */
HTH
viza
Jun 27 '08 #3
On May 19, 1:54 pm, viza <tom.v...@gmail.comwrote:
Hi

On May 19, 11:38 am, pereges <Brol...@gmail.comwrote:
: warning: the `gets' function is dangerous and should not be used.
char zf_name[50];
gets(zf_name);

How do you know the file name is less than 50 characters?

Use:
char zf_name[ SOME_CONSTANT ];
fgets( zf_name, SOME_CONSTANT, stdin );

This will at least not overflow your buffer.

You should also do:

if( ferror( stdin )) /* there was a read error */
Does not necessarily mean there was a read error. It could have been
that EOF was reached.
if(!feof(stdin) && ferror(stdin)) means an error happened.
if( zf_name[ strlen(zf_name) - 1 ] == '\n' )
First check that strlen(zf_name) 0
zf_name[ strlen(zf_name) - 1 ] = 0; /* take the newline character
off the filename */

else
/* line was longer than SOME_CONSTANT, and part of it is missing */
Not necessarily again. Most of the times that's what it means. If EOF
wasn't reached, you can try to read a byte from stdin with getc. If
getc returns EOF, check the stream if feof(). If feof() returns a
positive value, then the line wasn't longer than SOME_CONSTANT (unless
ferror() returns positive). If getc() does not return EOF, ungetc the
return value, and in that case yes; the line was longer than
SOME_CONSTANT.
Jun 27 '08 #4
viza wrote:
Hi

On May 19, 11:38 am, pereges <Brol...@gmail.comwrote:
>: warning: the `gets' function is dangerous and should not be used.
char zf_name[50];
gets(zf_name);


How do you know the file name is less than 50 characters?

Use:
char zf_name[ SOME_CONSTANT ];
fgets( zf_name, SOME_CONSTANT, stdin );

This will at least not overflow your buffer.

You should also do:

if( ferror( stdin )) /* there was a read error */
Why not check the value returned by fgets which will catch both an input
error and an end-of-file? Then, if you need to distinguish between them
can either ferror or feof.

If there was either an error or an end-of-file you want to avoid
executing the code below.
if( zf_name[ strlen(zf_name) - 1 ] == '\n' )
zf_name[ strlen(zf_name) - 1 ] = 0; /* take the newline character
off the filename */

else
/* line was longer than SOME_CONSTANT, and part of it is missing */
and maybe also:

if( feof( stdin )) /* end of file was reached */
Well, that would need to be done up above when checking ferror rather
than down here (which is probably what you intended but might not be
obviouse to the OP).
--
Flash gordon
Jun 27 '08 #5
Will this suffice:

if(scanf("%s", zf_name) != 1)
{
perror("Erroneous file name!");
exit(EXIT_FAILURE);
}
Or should I also check if the array limit is not exceeded ?
Jun 27 '08 #6
On May 19, 5:03*am, pereges <Brol...@gmail.comwrote:
Will this suffice:

if(scanf("%s", zf_name) != 1)
As coded, this is no better than fgets. There are modifiers you can
place between the % and the s which will prevent buffer overflow but
it becomes even harder to determine if you received the entire file
name. fgets is still the easiest to use.
{
* * perror("Erroneous file name!");
* * exit(EXIT_FAILURE);

}

Or should I also check if the array limit is not exceeded ?
You cannot check this after the fact. Once the limit is exceeded you
are in the realm of undefined behavior.

Jun 27 '08 #7
On May 19, 5:23 pm, Barry Schwarz <schwar...@yahoo.comwrote:
On May 19, 5:03 am, pereges <Brol...@gmail.comwrote:
Will this suffice:
if(scanf("%s", zf_name) != 1)

As coded, this is no better than fgets. There are modifiers you can
place between the % and the s which will prevent buffer overflow but
it becomes even harder to determine if you received the entire file
name. fgets is still the easiest to use.
{
perror("Erroneous file name!");
exit(EXIT_FAILURE);
}
Or should I also check if the array limit is not exceeded ?

You cannot check this after the fact. Once the limit is exceeded you
are in the realm of undefined behavior.
I agree. What about the following code ?

if(fgets(zf_name, MAX_SIZE, stdin) == NULL)
{
if(ferror(stdin))
{
perror("Error while reading file name!");
exit(EXIT_FAILURE);
}
if(feof(stdin))
{
perror("End of file reached!");
exit(EXIT_FAILURE);
}
}

Also I'm wondering about the new line character which makes fgets
stops reading but is included in the string copied to zf_name.
Shouldn't we deal with it too ?
Jun 27 '08 #8
On May 19, 4:33 pm, pereges <Brol...@gmail.comwrote:
On May 19, 5:23 pm, Barry Schwarz <schwar...@yahoo.comwrote:
On May 19, 5:03 am, pereges <Brol...@gmail.comwrote:
Will this suffice:
if(scanf("%s", zf_name) != 1)
As coded, this is no better than fgets. There are modifiers you can
place between the % and the s which will prevent buffer overflow but
it becomes even harder to determine if you received the entire file
name. fgets is still the easiest to use.
{
perror("Erroneous file name!");
exit(EXIT_FAILURE);
}
Or should I also check if the array limit is not exceeded ?
You cannot check this after the fact. Once the limit is exceeded you
are in the realm of undefined behavior.

I agree. What about the following code ?

if(fgets(zf_name, MAX_SIZE, stdin) == NULL)
{
if(ferror(stdin))
{
perror("Error while reading file name!");
exit(EXIT_FAILURE);
}
if(feof(stdin))
{
perror("End of file reached!");
exit(EXIT_FAILURE);
}

}

Also I'm wondering about the new line character which makes fgets
stops reading but is included in the string copied to zf_name.
Shouldn't we deal with it too ?
All your questions are answered in my previous post.
Jun 27 '08 #9
vi******@gmail.com writes:
On May 19, 1:54 pm, viza <tom.v...@gmail.comwrote:
[...]
>if( ferror( stdin )) /* there was a read error */
Does not necessarily mean there was a read error. It could have been
that EOF was reached.
Um, yes, ferror(stdin) does mean that there was a read error.
if(!feof(stdin) && ferror(stdin)) means an error happened.
That means that either an error or an end-of-file condition happened.

[snip]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #10
Keith Thompson said:
vi******@gmail.com writes:
>On May 19, 1:54 pm, viza <tom.v...@gmail.comwrote:
[...]
>>if( ferror( stdin )) /* there was a read error */
Does not necessarily mean there was a read error. It could have been
that EOF was reached.

Um, yes, ferror(stdin) does mean that there was a read error.
>if(!feof(stdin) && ferror(stdin)) means an error happened.

That means that either an error or an end-of-file condition happened.
It does? I'd have thought that it meant that both an error happened and an
end-of-file condition /didn't/ happen. What am I missing?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #11
vi******@gmail.com wrote:
On May 19, 1:54 pm, viza <tom.v...@gmail.comwrote:
>>
>if( ferror( stdin )) /* there was a read error */
Does not necessarily mean there was a read error.
That's wrong.
It could have been that EOF was reached.
if(!feof(stdin) && ferror(stdin)) means an error happened.
I'm guessing that you don't know
what the return value of feof means either.

N869
7.19.10.2 The feof function
Returns
[#3] The feof function returns nonzero if and only if the
end-of-file indicator is set for stream.

7.19.10.3 The ferror function
Returns
[#3] The ferror function returns nonzero if and only if the
error indicator is set for stream.

--
pete
Jun 27 '08 #12
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:
>vi******@gmail.com writes:
>>On May 19, 1:54 pm, viza <tom.v...@gmail.comwrote:
[...]
>>>if( ferror( stdin )) /* there was a read error */
Does not necessarily mean there was a read error. It could have been
that EOF was reached.

Um, yes, ferror(stdin) does mean that there was a read error.
>>if(!feof(stdin) && ferror(stdin)) means an error happened.

That means that either an error or an end-of-file condition happened.

It does? I'd have thought that it meant that both an error happened and an
end-of-file condition /didn't/ happen. What am I missing?
Nothing. I just wasn't paying attention. Whoops.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #13
On May 19, 7:20 pm, pete <pfil...@mindspring.comwrote:
vipps...@gmail.com wrote:
On May 19, 1:54 pm, viza <tom.v...@gmail.comwrote:
if( ferror( stdin )) /* there was a read error */
Does not necessarily mean there was a read error.

That's wrong.
It could have been that EOF was reached.
if(!feof(stdin) && ferror(stdin)) means an error happened.

I'm guessing that you don't know
what the return value of feof means either.

N869
7.19.10.2 The feof function
Returns
[#3] The feof function returns nonzero if and only if the
end-of-file indicator is set for stream.

7.19.10.3 The ferror function
Returns
[#3] The ferror function returns nonzero if and only if the
error indicator is set for stream.
Oh! I did not know about that. Thanks a lot. I wonder just how much of
my C knowledge is wrong.
Regardless, the OP should still use both feof and ferror to find out
which happened when getc returned EOF.
Jun 27 '08 #14
On May 19, 10:02 pm, vipps...@gmail.com wrote:
Oh! I did not know about that. Thanks a lot. I wonder just how much of
my C knowledge is wrong.
Regardless, the OP should still use both feof and ferror to find out
which happened when getc returned EOF.
where did getc come into picture ? i guess its a part of fgets
implementation ?
Jun 27 '08 #15
pereges wrote:
>
I'm getting a warning on my linux gcc compiler:

/tmp/ccXgHa9s.o(.text+0x48): In function `main':
: warning: the `gets' function is dangerous and should not be used.
And you should pay attention and not use it. The simplest
replacement is ggets, which is written in standard C and you can
simply compile and link it to your program. See:

<http://cbfalconer.home.att.net/download/ggets.zip>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #16
Barry Schwarz wrote:
pereges <Brol...@gmail.comwrote:
Will this suffice:

if(scanf("%s", zf_name) != 1)

As coded, this is no better than fgets. ...
YM gets().

--
Peter
Jun 27 '08 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Adam Hartshorne | last post by:
Hi All, I was wonder if somebody could tell me about this warning, as I have written a fairly large application and I am gettting lots and lots of these warning. I have no real clue what they...
10
by: paytam | last post by:
hi all can you tell me what's the wrong with this code? I use gcc compiler,but when I wanted to use gets() function in my code but it takes a dangerous warning(the gets function is dangerous...
5
by: Tom | last post by:
I have a function that restricts access to a page to logged in users. When a user who isn't logged in goes to the page, it will dynamically generate a login form. I'm trying to use it in...
4
by: metalinc | last post by:
hi...im new to C programming...need help...tried to run this code but got this error fork.c: In function ‘parse’: fork.c:44: warning: comparison between pointer and integer fork.c:51: warning:...
2
by: ollii | last post by:
Hello everybody im just creating an client that is going to communicated with Iterative server. and i get this error: warning: the `gets' function is dangerous and should not be used. and i...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
1
by: desivirus | last post by:
hi admin.. i followed your tip in "HOW TO LIST PROCESS ID IN WINDOWS" thread..and iam trying to compile this code in cygwin , $gcc -mno-cygwin process.c -o -L"psapi.lib" process.exe psapi.h...
4
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
5
by: qwert7465 | last post by:
any help would be appreciated. here is my code: // Final Project #include <iostream.h> #include <iomanip.h> void checkout();
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.