Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 17, 2005, 07:00 AM   #1
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
'gets' considered unsafe?

Here is a sample code
Quote:
#include <iostream>
#include <cstdio>

using namespace std;

int main(){
int t, i;
char text[100][80];

for (t=0; t<100; t++){
cout << t << "; ";
gets(text[t]);
if(!text[t][0]) break;
}
for (i=0; i<t; i++)
cout << text[i] << '\n';
return 0;
}
When I compile this code, although it compiles correctly, xCode gives me a message at the start of the executable file "warning: this program uses gets(), which is unsafe.". Why? Although I can run the program flawlessly, that warning bothers me, because don't know if this command will cause problems when I try to write other programs.

Oh, and how can I create a box that contains code in this forums? (you know, just like the box of a quote, but with code).
Soulstorm is offline   0 Reply With Quote
Old Apr 17, 2005, 07:26 AM   #2
Mitthrawnuruodo
Moderator
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
From C/C++ Programmers Reference: "There is no way to limit the number of characters that gets() will read, which means that the array pointed to by str could be overrun. Thus, this function is inherently dangerous. Its use should be limited to sample programs or utilities for your own use. It should not be used for production code."
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Apr 17, 2005, 11:53 AM   #3
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
ok thanks! This "programmer reference"... What is it? Just another book?

Sorry if my question sounds newbish...
Soulstorm is offline   0 Reply With Quote
Old Apr 17, 2005, 12:17 PM   #4
broken_keyboard
Banned
 
broken_keyboard's Avatar
 
Join Date: Apr 2004
Location: Secret Moon base
You can tell without the book. Look at the function call, you are passing an array without any indicator of it's size. How can the function possibly know how many chars are pointed to? There is no way.

The man page for gets recommends using fgets instead...
broken_keyboard is offline   0 Reply With Quote
Old Apr 17, 2005, 12:44 PM   #5
Mitthrawnuruodo
Moderator
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
Quote:
Originally Posted by Soulstorm
ok thanks! This "programmer reference"... What is it? Just another book?
Not, "just another book", but "The Most Authorative Quick Reference for C/C++ Programmers", according to the Publisher... don't know about that, but it's VERY handy when programming and really cheap, too...
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Apr 18, 2005, 05:51 AM   #6
GeeYouEye
macrumors 68000
 
GeeYouEye's Avatar
 
Join Date: Dec 2001
Location: State of Denial
Send a message via AIM to GeeYouEye Send a message via Yahoo to GeeYouEye
WAY off topic, but...

soulstorm: your signature is inaccurate: iPods have 32 MB of RAM.
__________________
I bring order to chaos. You are in chaos Windows, you are the contradiction, a bug wishing to be an OS.
Visit Softyards Software
NEW DEFINITION OF GEEK
Like politics, free speech, computers, entertainment, and more? Join us at Wordforge.net
GeeYouEye is offline   0 Reply With Quote
Old Apr 18, 2005, 01:55 PM   #7
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by GeeYouEye
WAY off topic, but...

soulstorm: your signature is inaccurate: iPods have 32 MB of RAM.
Didn't know that my iPod had RAM. Come to think of it, how it kept in its memory some songs to be played? Should have noticed it. Anyway, I fixed my sig. Thanks
Soulstorm is offline   0 Reply With Quote
Old Apr 19, 2005, 10:46 AM   #8
SilentPanda
Moderator emeritus
 
SilentPanda's Avatar
 
Join Date: Oct 2002
Location: The Bamboo Forest
To encapsulate code in this forum (and maybe others) you simply type CODE in the []'s instead of QUOTE.
__________________
My 24 hour web cam! ʕノᴥʔノ ︵ ┻━┻
SilentPanda is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Employees and unsafe working conditions samiwas Politics, Religion, Social Issues 103 Apr 12, 2014 12:56 PM
Is your Mac Pro unsafe? slughead Mac Pro 72 Feb 9, 2013 11:25 AM
warning: this program uses gets(), which is unsafe. Mugambo Mac Programming 5 Jul 26, 2012 11:13 AM

Forum Jump

All times are GMT -5. The time now is 04:12 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC