'gets' considered unsafe?

Discussion in 'Mac Programming' started by Soulstorm, Apr 17, 2005.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    Here is a sample code

    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).
     
  2. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #2
    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."
     
  3. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    ok thanks! This "programmer reference"... What is it? Just another book?

    Sorry if my question sounds newbish...
     
  4. macrumors 65816

    broken_keyboard

    Joined:
    Apr 19, 2004
    Location:
    Secret Moon base
    #4
    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...
     
  5. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #5
    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... ;)
     
  6. macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #6
    WAY off topic, but...

    soulstorm: your signature is inaccurate: iPods have 32 MB of RAM. ;)
     
  7. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #7
    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 ;)
     
  8. Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #8
    To encapsulate code in this forum (and maybe others) you simply type CODE in the []'s instead of QUOTE.
     

Share This Page