Thread: gets()

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    32

    gets()

    Is there an equal command to gets() in C. I am storing a string into a character array and am using gets(). So I have something like:
    Code:
    char dBuffer[100];
    
    gets(dBuffer);
    I would just like to know if this is about it or if there is another function that will do the same thing.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    there is a gets() function in C but it is very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very bad. Read this to find out why

    A much better function is fgets().

    From the man page:
    BUGS
    Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and
    because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to
    break computer security. Use fgets() instead.
    fgets() works in basically the same fashion as gets() but it is a lot safer.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Just read your link. I suppose buffer overflows might just be the thing to ruin my day
    -
    I just switched to fgets(), Thanks

  4. #4
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41
    >It has been used to break computer security.

    can you explain how you can do that ?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    If you're interested in knowing how overflows work, I highly recommend this document:

    http://www.insecure.org/stf/smashstack.txt
    Jason Deckard

Popular pages Recent additions subscribe to a feed