LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2002, 11:07 AM   #1
herambshembekar
Member
 
Registered: Feb 2002
Location: India
Distribution: redhat 7.1
Posts: 70

Rep: Reputation: 15
gets() function is dengerous !!


I got message
the 'gets' function is dengerous & shoud not be used i am using
gcc - 2.96
i ahve also tried on gcc(kgcc) -2.91.66

HELP!
 
Old 03-03-2002, 10:31 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
The message was a warning from the compiler. gets is a dangerous method because gets does not know the size of the buffer passed in. Take a look at this sample program:
Code:
#include <stdio.h>
main()
{
   char szBufferOverrun[5];

   if (( gets( szBufferOverrun )) == NULL )
   {
      perror( "gets failed" );
   } 
   printf( "szBufferOverrun is [%s]\n", szBufferOverrun );
}
How well this program run is completely up to the user filling up the gets buffer. If you run this program and type in < 5 charcters the program is fine. If you type in > 5 characters, all bets are off. When I ran the program and typed in about 30 characters, the program printed out my string, then seg faulted.
Buffer overruns are a serious problem and a small bug, in a program, like the one in my example can often comprimise the security of an entire machine.
It is better to use a method like fgets and pass in STDINFILENO as the file pointer instead of using gets.

Last edited by crabboy; 03-03-2002 at 10:32 PM.
 
Old 03-09-2002, 09:45 AM   #3
herambshembekar
Member
 
Registered: Feb 2002
Location: India
Distribution: redhat 7.1
Posts: 70

Original Poster
Rep: Reputation: 15
Wink Thanks

Thanks crabboy
 
Old 03-11-2002, 03:02 PM   #4
tyler_durden
Member
 
Registered: May 2001
Posts: 125

Rep: Reputation: 15
use fgets() instead
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Is the wait function is the same as the sleep function ? Linh Programming 3 04-28-2004 12:39 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration