LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-15-2003, 12:07 PM   #1
OrganicX
Member
 
Registered: Aug 2002
Posts: 105

Rep: Reputation: 15
A question about the gets() function in C


I was writting a silly little program the other day and used the gets() function to capture user input from the keyboard. The code compiled successfuly except for one warning, warning me that the gets() function is dangerous. The compiler advised that I use something besides gets() in the fututre.
I dont know what else to use so I use gets() and it works fine.

What can I use to capture strings from
the keyboard besides gets()?

Why does the compiler think it's so dangerous?


ex.

char x[51];

printf("\nenter a string:");
gets(x);
 
Old 03-15-2003, 12:24 PM   #2
tda
Member
 
Registered: Mar 2002
Location: Ukraine, Kiev
Distribution: Mandrake 7.2
Posts: 37

Rep: Reputation: 15
The gets() function doesn't know a size of a result buffer which is used for readed string. Therefore if this buffer will be small for input string it can corrupt program. Instead you can use fgets() or scanf().
 
Old 10-20-2003, 12:27 PM   #3
Clyde S
LQ Newbie
 
Registered: Oct 2003
Location: Maumelle, AR
Posts: 1

Rep: Reputation: 0
Using your example, try the following to get around the buffer overflow problem:

char x[51];

printf("\nenter a string:");
fgets(x, sizeof(x)-1, stdin);

This passes a max buffer size to the fgets function.

Good Luck,

Clyde
 
Old 10-20-2003, 01:11 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by tda
Instead you can use fgets() or scanf().
Yes, you can use scanf(), but if you do not specify a max filedwidth for "%s", or use automatic allocation ("%as"), it is dangerous the same way as gets().
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
A question about the listen function Nerox Programming 3 07-31-2005 09:06 PM
A question about connect() function of socket realos Programming 4 07-27-2005 03:21 PM
Question about the find function in Kate LaptopLinux Linux - Software 2 06-27-2005 08:32 AM
simple python function question Tyir Programming 2 03-28-2004 10:53 PM
Question about function prototypes xailer Programming 4 12-15-2003 10:10 AM

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

All times are GMT -5. The time now is 04:21 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