Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 26, 2012, 06:22 AM   #1
Mugambo
macrumors 6502
 
Join Date: Jul 2009
warning: this program uses gets(), which is unsafe.

For the following program, xcode compiles and runs the program fine but gives the aforementioned error. Why is it bad to use gets()?

Here is the program for which xcode gave the error:

Converting lowercase string to uppercase:


Code:
#include <stdio.h>

int main(int argc, const char * argv[])
{

    // insert code here...
    char str[100], i;
    printf("Enter a string");
    gets(str);
    for(i=0;i<100 ;  )
    {
        if((str[i]>=97)&&(str[i]<=123))
            str[i]-=32;
        i++;
    }
    printf("%s", str);
    return 0;
}

Last edited by OllyW; Jul 26, 2012 at 08:08 PM. Reason: Please use code tags
Mugambo is offline   0 Reply With Quote
Old Jul 26, 2012, 06:35 AM   #2
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
You should never use gets() as it is a dangerous function. The reason is that it does not allow you to state how big your buffer is so it is very easy to get a buffer overflow when using it.

Use fgets() instead.

Edit: In C11 the gets() function has been removed from the standard completely and if compiling in strict C11 mode it should not even exist (although I have a nasty feeling that most implementations will continue to support it even though it has been removed).
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   1 Reply With Quote
Old Jul 26, 2012, 07:06 AM   #3
Mugambo
Thread Starter
macrumors 6502
 
Join Date: Jul 2009
Thank you for the explanation.
I replaced gets() with fgets() and the program fails to build.
Any help please.
Mugambo is offline   0 Reply With Quote
Old Jul 26, 2012, 07:09 AM   #4
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
Quote:
Originally Posted by Mugambo View Post
Thank you for the explanation.
I replaced gets() with fgets() and the program fails to build.
Any help please.
fgets() has a different signature than gets(). Read the documentation for it by typing the following in a terminal window:

Code:
man fgets
this will explain how to use the fgets() function.
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old Jul 26, 2012, 07:13 AM   #5
Mugambo
Thread Starter
macrumors 6502
 
Join Date: Jul 2009
Great! Thanks again!
Mugambo is offline   0 Reply With Quote
Old Jul 26, 2012, 12:13 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by Mugambo View Post
Great! Thanks again!
I just wanted to point out that both questions, "Why is gets unsafe?", and "Use of fgets", could have been answered by googling the text of the question. Go ahead, try it.

It's good to get used to finding answers yourself, even ones you think you might not find an answer for.
chown33 is offline   2 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 01:56 PM
[Help]Apple Developer Program Enrollment Warning! Oneill App Store Business, Legal and Marketıng 3 Sep 29, 2013 12:43 PM
Is your Mac Pro unsafe? slughead Mac Pro 72 Feb 9, 2013 12:25 PM

Forum Jump

All times are GMT -5. The time now is 03:40 AM.

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

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