Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > OSX C doesn't like gets()

OSX C doesn't like gets()
Thread Tools
Mac Enthusiast
Join Date: Jan 2001
Location: ny ny usa
Status: Offline
Reply With Quote
Apr 25, 2002, 12:26 AM
 
I am learning C from a book called 'Pointers On C' by kenneth A. Reek.

In the first example of the book he uses the gets() function. When the program executes the gets() function for the first time it outputs:

warning: this program uses gets(), which is unsafe.

note: the code compiles fine with cc.

PS. I am learning C so that I can eventually learn Cocoa.
'Satisfy the urge and discover the need' Q-Tip
     
Dedicated MacNNer
Join Date: Oct 2000
Status: Offline
Reply With Quote
Apr 25, 2002, 03:24 AM
 
Originally posted by muchfresh:
<STRONG>warning: this program uses gets(), which is unsafe.
</STRONG>
This from "The Standard C Library" (1992) by P. J. Plauger:

""Avoid using this function. You have no way to limit the number of characters it reads. Use fgets instead."

char *gets(char *s): The gets function reads characters from the input stream pointed to by stdin, into the array pointed to by s, until end-of-file is encountered or a new-line character is read. Any new-line character is discarded and a null character is written immediately after the last character read into the array.

char *fgets(char *s, int n, FILE *stream): The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.
     
Mac Enthusiast
Join Date: Jan 2001
Location: ny ny usa
Status: Offline
Reply With Quote
Apr 25, 2002, 08:33 AM
 
You rock!

I guess I picked a bad book. I just wanted a book that focused on pointers.
'Satisfy the urge and discover the need' Q-Tip
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 25, 2002, 09:40 AM
 
I've heard a lot of good things about this book but yah, gets is one thing you should avoid.

If there's one other tip I may mention (I only started doing this lately), use a lot of asserts in your program, e.g.:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#include &lt;assert.h&gt;
...
void moo(char *p)
{
assert(p && <font color = red>"Ick, p is NULL"</font>);
}
</font>[/code]

Even with functions that take indices for arguments, I love asserting that those indices are within bounds.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Forum Regular
Join Date: Oct 2000
Status: Offline
Reply With Quote
May 3, 2002, 05:05 AM
 
Ahhh, the benefits of a sound introductory operating systems course. From Tanenbaum, "Modern Operating Systems" in the chapter on security:

One rich source of attacks has been due to the fact that virtually all operating systems and most systems programs are written in the C programming language ... Unfortunately, no C compiler does any array bounds checking. Consequently, the following code sequence, while not illegal, is not also checked:

int i;
char c[1024];
i = 12000;
c[i] = 0;

The result is that some byte of memory 10,976 bytes outside the array c is overwritten, possibly with disastrous consequences. No check is performed at runtime to prevent this error.
...

This [buffer overflow] ... works with very long environment strings, user input, or anything else where the programmer has created a fixed-size buffer to handle a user-supplied string that was expected to be short. By providing a long handcrafted string containing a program, it may be possible to get the program onto the stack and then get it executed.

The C library function "gets" which reads a string (of unknown size) into a fixed-size buffer, but without checking for overflow, is notorious for being subject to this kind of attack. Some compilers even detect the use of "gets" and warn about it.
So there you have it, gcc's just being cute. Well actually, it's preventing you from getting hacked by some malicious programmer with an intricate knowledge of assembly and the stack structure of UNIX and the way your user program, when running, is assigned an address space, but... it's probably just better to use fgets() so your program doesn't crash with a buffer overflow.

[ 05-03-2002: Message edited by: Chaaaosss ]
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:53 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2