Thread: gets() not so bad

  1. #46
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    I agree that gets() must be gotten rid of. Unless you give it an infinitely large buffer, it's always possible for the user to enter a line longer than the buffer.

    Besides, is this:

    Code:
    fgets(buffer, sizeof(buffer), stdin)
    really that much harder to type than this:

    Code:
    gets(buffer)
    ?

    Quote Originally Posted by Bayint Naung View Post
    Probably they don't know there are free compiler + necessary complete tools
    gcc + gdb + splint + (yacc|bison) + flex + valgrind + cproto + indent + cdecl + etc.
    Quote Originally Posted by MK27 View Post
    This is only a problem in "real world" settings like MS where economics and scale have made $$$ the priority, so quality of the product (and the people who produce it) is second seat to the quantity they can produce. That's what needs to be fixed.
    Linux rules!


    Quote Originally Posted by iMalc View Post
    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!
    If you used Linux, you wouldn't need to restart all the time!
    Last edited by MTK; 06-11-2010 at 06:32 PM.

  2. #47
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    But, but...then you have to remove the newline if it's there! And if it's not there, well then, now you have to copy what you've got somewhere else and read again! That's CRAZY talk!

  3. #48
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I'm personally in favour of teachers discouraging use of gets().

    The point of teaching a skill is to give the student enough knowledge that they can do something practically, without running too much risk of getting in trouble or causing harm.

    With most scientific or engineering fields, the teaching is initially based on a small set of tools and techniques. Those tools and techniques may have limits, but they can be well understood, and the student can progress to more advanced tools or techniques once s/he knows enough to use the basic tools safely and has learnt enough discipline that they will not be inherently dangerous with an advanced tool.

    A new chemistry student is not just pushed with an oxy-acetelene kit into a room with a swimming pool full of nitric acid and told to do experiments. He or she is taught some basic theory first, taken carefully through the use of lab basic equipment (beakers, bunsen burners, fume cupboards) and initially exposed to chemicals that are not harmless, then small amounts on more dangerous chemicals (sodium, magnesium, etc). As their skills build up, they are taught and allowed to do more advanced - and potentially dangerous - experiments.

    Contrast this with teaching of computer programming. Students are often armed with potentially dangerous tools, told little about their dangers, and encouraged to experiment with them. Now, OK, they are not expected to write software to control a pacemaker on their first day, but the techniques or functions they are taught to use are often dangerous and actively discouraged in any professional setting - eventually they'll have to learn safer alternatives. gets() is a prime example of such a function.

    I'm all for advanced students and practitioners having access to potentially dangerous tools. There is nothing worse than coddling a talented, knowledgeable, professional. However, I do not support giving those tools to beginners who don't understand them. One characteristic of beginners - particularly those who have interest or potential to eventually become great practitioners - is that they experiment and push the boundaries. If the tool they are given is inherently unsafe, they will experiment with it, and eventually use it in an unsafe manner without guidance, without any knowledge of what they are doing wrong, and get in trouble. The only way to mitigate those risks is to avoid giving them those dangerous tools before they are ready for them.
    Last edited by grumpy; 06-11-2010 at 07:16 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #49
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    This whole conversation reminds me of this quote:

    Quote Originally Posted by http://en.wikipedia.org/wiki/Unix_philosophy
    "UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things." – Doug Gwyn
    And that's why I think that things like memcpy, etc should NOT be removed. Unlike gets(), they CAN be safe if used right.

  5. #50
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I don't think that Elysia is calling for a removal of any of the functions (except maybe gets), but an addition of some functions that are to be safer. Anyway, since we are talking about adding and removing functions from the standard, why the heck don't we see something like Gnu getline added to the standard?

  6. #51
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, that's a good question, but I notice that many standard things do not necessarily rely on malloc like GNU getline does. To the end of letting people use whatever allocator libraries they really need to use, which has its own auspices. There's stuff like fopen that could obviously be implemented with other parts of the standard but that is not the point.
    Last edited by whiteflags; 06-11-2010 at 08:00 PM.

  7. #52
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Successful Troll was successful.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #53
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    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.
    memcpy is not safe. An additional parameter of how much to copy isn't safe.
    What if you're copying X bytes of buffer Y into offset A in buffer B?
    Then sizeof(dst) will give you a buffer overrun. You have to calculate the correct amount of bytes to copy, and there's the problem. You will screw that up one time or another.

    Quote Originally Posted by kermit View Post
    I don't think that Elysia is calling for a removal of any of the functions (except maybe gets), but an addition of some functions that are to be safer. Anyway, since we are talking about adding and removing functions from the standard, why the heck don't we see something like Gnu getline added to the standard?
    Not removal. Addition.
    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.

  9. #54
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    With memcpy(), you can specify how many bytes to copy. Programmer can make sure there's no overflow. with gets(), it's not.
    It's programmers' responsibility to check there's enough space in destination.
    Last edited by Bayint Naung; 06-12-2010 at 03:56 AM.

  10. #55
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    memcpy is not safe. An additional parameter of how much to copy isn't safe.
    I disagree, because like you, I disagree with the notion that "we should ban C, probably C++ and many other languages because they're unsafe". As such, I think that a more useful notion of safety should regard memcpy as safe, since it is safe when used correctly, which can be done in conjunction with better programming practices.

    Quote Originally Posted by Elysia
    What if you're copying X bytes of buffer Y into offset A in buffer B?
    Then sizeof(dst) will give you a buffer overrun. You have to calculate the correct amount of bytes to copy, and there's the problem. You will screw that up one time or another.
    The problem is that programmers make mistakes. As such, this calls for practices such as automated testing, code review and pair programming.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #56
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Bayint Naung View Post
    It's programmers' responsibility to check there's enough space in destination.
    If you had been following the debate, you would know that this is the problem. We make mistakes.
    However, a computer is deterministic. Therefore, once you create security checks that works, they will always work. No more buffer overruns.

    Quote Originally Posted by laserlight View Post
    I disagree, because like you, I disagree with the notion that "we should ban C, probably C++ and many other languages because they're unsafe". As such, I think that a more useful notion of safety should regard memcpy as safe, since it is safe when used correctly, which can be done in conjunction with better programming practices.
    Still, if there is a way to make it safer, then it's a win situation.
    How many times have people here messed up the size parameter to any function like this? Oh, sure, we do it all the time. We fix it after some debugging.
    Now that is dangerous.

    The problem is that programmers make mistakes. As such, this calls for practices such as automated testing, code review and pair programming.
    Absolutely. But where is the harm in adding an extra security layer to catch more bugs before they propagate? The earlier a bug is caught in the development process, the better. For both the company and the end users.
    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.

  12. #57
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    But where is the harm in adding an extra security layer to catch more bugs before they propagate?
    Indeed, but the problem with Microsoft's additions is that "the secure functions do not prevent or correct security errors; rather, they catch errors when they occur". We should catch the errors before they occur.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #58
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, that's true. That would be the best thing.
    But we also need some security, some kind of post-immunization that minimizes the damage that should occur in the field in form of uncaught security bugs.
    Microsoft's additions are not the best. But they do aid in catching some bugs. I'd rather see functions fail instead of throwing assertions too, though.
    And more checks wouldn't be a bad idea. If such a thing were possible.
    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. #59
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Quote Originally Posted by Elysia View Post
    Still, if there is a way to make it safer, then it's a win situation.
    How many times have people here messed up the size parameter to any function like this? Oh, sure, we do it all the time. We fix it after some debugging.
    Now that is dangerous.
    One little project I did involved a lot of dealing with allocated memory and it tried to write to places it wasn't supposed to all the time.

    The worst that happened was just a little message in the terminal saying "Segmentation Fault" and the program quitting, no harm done.

  15. #60
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That just shows how naive you are about security.
    Do you want your program to crash on users?
    Furthermore, how about those bugs that doesn't cause a crash? The invisible ones that leaves your computer wide open to hacker attacks?
    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.

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