Thread: gets() not so bad

  1. #31
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    ...And one day you'll shoot yourself in the foot because you didn't think right. You introduced a bug. The bug didn't crash the program and you got a buffer overrun. Yay.
    ...Of course, this could have been prevented in the first place had you used safety checks.
    Can happen, but:

    1) I use a memchecker.
    2) I'm very aware of the issue, so I'm used to dealing with it. If you want some tips just ask!

    Analogy: If no one ever takes the training wheels off your bike, it will be hard for you to ride around without them.

    Does that mean it is oh so hard for people to ride around with no training wheels?!?? Hmmm...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The keyword here is: can.
    Maybe you forgot to run your memchecker one day. Maybe it's part of some rarely executed code so your memchecker missed it.
    The possibilities are endless.

    Learning to program safely is no substitute for security checks. They can augment them and increase security, but removing the security checks will hurt security.
    And besides us veterans, there are newbies. They constantly make mistakes. Security checks are vital for these people.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    The possibilities are endless.
    Evidently

    Bug are made for fixin'. Or else this is why people should not write code for computers.
    Last edited by MK27; 06-11-2010 at 11:07 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Now that would be sensible.
    Computers building computers and computers writing computer software
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    Now that would be sensible.
    Computers building computers and computers writing computer software
    This could lead to a personal existential crisis if drinking beer full time does not prove fulfilling enough. Also I don't have very many uses for computers beyond programming them

    Oh and keeping all my .mp3's in order.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #36
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Elysia
    It's the right thing to do to ban gets. Now I also hope they will consider adding safe variants of other unsafe (but rarely mentioned) functions such as strcpy, memcpy, strcat, etc.
    I can hardly believe I am writing this, but I think I agree with MK27..

    You can't idiot proof everything (though unfortunately we live in a society that tries). I mean, why do we need labels on lawnmowers that indicate to the user *not* to pick it up by grabbing under each side of the deck, when the motor is running!!?? You just know those labels are there because someone, who ostensibly is not too bright, did just that. I can imagine a time when things like lawnmowers will be banned, because they are just too "dangerous" (ignoring the fact that people have safely used them for years). But why do we need to idiot proof everything? (I know part of it has to do with covering one's behind). Why can't we, as the saying goes, put a label on stupid people? I would rather see people understanding that there are consequences for doing certain things, and to use their own judgement and exercise caution when doing them. In other words, I want people to be free to do what they want, even if it is stupid.

    But as for changing the string functions in the standard C library, Elysia, surely you don't feel that these potential pitfalls are anywhere near the same as using gets, in terms of being "dangerous" in a program. If used judiciously, the string functions are no problem. They have been in safe effective use for years. gets is another story. As it is said, it cannot be used safely; this is so, because no matter how careful the programmer is, it is the user who will botch things up. The problem with gets is not at all like the problems that one might encounter with the string functions.

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course there's such a thing as too much, but for buffer overruns, I don't believe so.
    Sure, if we would want to foolproof as much as possible, we should ban C, probably C++ and many other languages because they're unsafe. But I don't think we should go to such extremes.

    This is all about statistics. In this case, gets has such a bad statistic that they ban it. But strcpy, memcpy, whatever have pretty bad buffer overrun statistics too, I would bet. Even though they may be safe to use under correct circumstances, they prove to be security risks since it's so easy to use them incorrectly.

    I would like to label users as stupid, too, but there must be an effort from both universities and the language. By adding (and not removing the old ones) safe string handling functions, and universities teaching these safer ones, we get safer code. Win-win.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It may be true that you can't bulletproof everything, however gets is not one of these things. Given the fact that we have fgets, gets is not only bad it's redundant. I don't think that if they banned gets() there would be a lot of tears over it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #39
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by nonoob View Post
    Can the regulars here stop deriding & insulting professors who teach students to use gets()?

    I use this function all the time. It's perfectly wonderful. Every program is likely to have way more places in them where things can blow up. The last thing a student of C needs to worry about is some orchestrated attack by someone who would exploit buffer overruns... Such users are not likely the audience for assignment project code.

    Not every piece of code needs to be criminal-hacker proof. Just make sure the buffer is reasonably large. Say, a reusable buffer of 500 bytes for simple inputs.
    Do you enjoy it when an attacker installs adware on your machine, or a keylogger to capture your passwords, or a when your harddrive is wiped clean unexpectedly? Do you like that fact that Windows updates are frequently required to be downloaded to your machine and your PC restarted?
    We don't!
    Lack of teaching security as part of programming is where the need for things like Windows updates has come from. When computers first came out whey were so large and expensive and hardly anybody knew how to program them. Their RAM was measured in KB etc ctc. They didn't have networks, and there weren't people out there who could make money off spam. Back then security was not really an issue. Things moved along a bit and we got home personal computers. These still typically weren't networked and their RAM was perhaps on the order of a few MB. A few people knew how to program them, but not many people stored vast amounts of critical data on them. And still, security was barely considered. The professors you speak of probably grew up around this era.
    At some point computers became common enough and networked and so easily programmable that security became a huge problem. People needed to learn that just because security had not been an issue in the past does not mean that it can be ignored now. Somewhere along the path to the modern PC you have got to realise that security needed to become a major focus, an integral part of programming itself. Those that have never been taught security as part of programming are the main problem. They continue to produce unsafe code to the detriment of the rest of us.

    Your argument is not merely about gets vs fgets. You want to go back to the days where security was not an issue. You want to do as your 10-20 year old C programming books say and use the functions that they claim you should use, because they're so awesomely correct. You at best believe that one first needs to learn how to program and then maybe they can learn a little about security on the side. This simply is not sufficient today!
    This needs to change and the current generation of wannabe programmers needs to learn security at every step along the way to becoming a real programmer. From the moment a new programmer first learns how to get input from the user or from a file, they need to be taught how to do it safely. Only then will we come close to having software that is seldom exploitable.

    When engineers are taught how to build a bridge, they are hopefully taught how to make something that is done correctly and will hold the weight it was designed for and withstand the forces of nature that are expected in the area, and then some etc. They aren't just taught to make something that will stay upright when you take away the temporary supports. It's just not enough. You wouldn't be given rubber supports to make a bridge out of, so why should you be given gets for accepting user input when we know it's crap?!

    If you don't have the time to learn how to program properly then pick another career. Perhaps become a lion-tamer, where it wont matter if you don't learn to be careful as you go.

    And no, Microsoft compilers are not the only ones to emit warnings whenever gets is used.
    Last edited by iMalc; 06-11-2010 at 03:26 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #40
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, a buffer of 500 bytes is not nearly large enough. When malicious input comes into play, it's usually way bigger than that. Oops, you're screwed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #41
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia
    This is all about statistics. In this case, gets has such a bad statistic that they ban it. But strcpy, memcpy, whatever have pretty bad buffer overrun statistics too, I would bet. Even though they may be safe to use under correct circumstances, they prove to be security risks since it's so easy to use them incorrectly
    One of these things is not like the others! (feel free to sing along!)
    Declaration:

    void *memcpy(void *str1, const void *str2, size_t n);

    Copies n characters from str2 to str1. If str1 and str2 overlap the behavior is undefined.

    Returns the argument str1.
    Now it would be better to say that it copies n bytes to str1. Anyway, you prevent buffer overflows by making sure str1 accommodates n, just like any other "safer" function. Also, believe it or not, I would like a function that just copies bytes.

    It may be mean of me to point this out but I'm glad that one person doesn't have the choice to redact things from the standards.

  12. #42
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can't call memcpy bad without calling pointers themselves bad.
    I.e. you can't avoid the problem it could cause without avoiding pointers altogether.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #43
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    Also, believe it or not, I would like a function that just copies bytes.
    Who says anything about removing it? It will still be there, in one form or another.

    It may be mean of me to point this out but I'm glad that one person doesn't have the choice to redact things from the standards.
    That is a good thing(TM).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #44
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Reminds me of that joke... "How many Microsoft employees does it take to change a light bulb?"
    "None, they just make darkness a standard."

    Microsoft have the history of renaming functions if they don't want you to use them (Think POSIX). There goes some of your portability. If it's in the standard, leave it alone, but feel free to give a big fat warning if someone is using gets(). If Microsoft find memcpy() "dangerous", then my goodness, don't let them do any string processing in C.

    Not to mention, the "safe" functions don't stop you from passing uninitialised pointers, or "the wrong pointers" to them.

  15. #45
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    Who says anything about removing it? It will still be there, in one form or another.


    That is a good thing(TM).
    I was more concerned that when faced with the truth -- that memcpy is safe contrary to your idea of what makes it unsafe -- that you would say it is useless and can be redacted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bad and fail of steam
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 02-19-2008, 03:07 AM
  2. Can you still view the bios screen with a bad CPU?
    By HyperCreep in forum Tech Board
    Replies: 4
    Last Post: 12-31-2006, 06:57 PM
  3. Replies: 6
    Last Post: 11-12-2005, 11:57 AM
  4. Bad coding habits
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-12-2005, 05:44 PM
  5. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 08:52 PM