Thread: gets() not so bad

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nonoob View Post
    Maybe programmers are getting dumber and we need to have nice bubble-wrapped soft and cozy functions for everything to protect us.
    That is actually the trend over recent years. Java, for example, had its genesis in being a "safer C++". In part because programmers expect things to "just work" (i.e. they don't take the care to avoid using constructs in an unsafe manner) and in part - like gets() - the constructs are inherently unsafe.

    Using gets() is all well and good if you can trust the user of your program not to do something silly that overruns the buffer length. In the real world, programmers have to be paranoid and assume the user of their program CANNOT be trusted to do the right thing. Teaching students otherwise is teaching them habits that will be frowned on when they go for a job.

    However, it is usually better to use some means that allows the programmer to prevent a buffer overrun. fgets() does that, without too much ado. Although there are trade-offs in its usage - extra care is needed to properly handle lines that exceed the buffer length. Not difficult, but care needed. In practice, the code is often less complicated by using fgetc() in a loop, if your code needs to read multiple lines.
    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.

  2. #17
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    gets() is your typical "trip on a landmine" C function.

    As for the deriding of instructors, I think that was referring to my recent post in another thread. However note, that the instructor I was deriding also encouraged students to fflush(stdin) and to use void main(). So, I am pretty justified in deriding him and will continue to do so.

    Furthermore, instructors like these that learn C at "band camp", then figure out that because they liked it they are ready to teach someone else, are dangerous elements for the profession in general.
    Last edited by claudiu; 06-10-2010 at 11:10 PM.
    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.

  3. #18
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by Bayint Naung View Post
    Never mind. Next C standard will remove gets() function.
    C1X - Wikipedia, the free encyclopedia
    Doesn't matter. Indian "profs" (kindergarten teachers no doubt) have their classes use Turbo C still and probably will for a long time.
    So they're stuck with a 30 year old language definition and a 20+ year old compiler that won't run properly on modern operating systems, and don't even understand why that's not a good thing.

    American and European schoolteachers are somewhat better, but still typically will use outdated tooling because it's cheaper and easier to get pirated copies of (and because the teaching material was written in 1995 or earlier and doesn't play nice with the more current tools, all the buttons in the IDE that it tells kids to press are in the wrong place now and might even have different icons on them).

  4. #19
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    because it's cheaper and easier to get pirated copies
    Probably they don't know there are free compiler + necessary complete tools
    gcc + gdb + splint + (yacc|bison) + flex + valgrind + cproto + indent + cdecl + etc.

  5. #20
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    If I could add my 2 cents, being a technical interviewer. I've seen post-doc students applying for C++ positions, who listed on their resume the fact that they taught first-year programming modules. So essentially people with no real world experience are teaching the next generation. What's worse is that these guys normally fail the interview as well, as they don't really understand what they're doing when it comes to memory management and pointers. Personally I'm scared of the people graduating in the next few years.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    Sounds a little more realistic than the jwenting fantasy which itself is stuck 20+ years in the past

    I am 100% sure this is not because people are failing to buy the proper MS products!
    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

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MK27 View Post
    I find that super-paranoia irritating too -- I presume it is a product of certain workplaces, and for good reason (but that does not make it "true" or justified in a more pure and abstract sense).
    Didn't we go over that last time? It's not super paranoia; it's security.
    Imagine Microsoft not using all those security checks. Well, we'd have a 1000x more patches today.
    Teach newbies to write safe code and do so early. It's growing all the more important in today's world. Even if they're not going to write real world applications in the future, they won't be happy if their machine gets hacked because they forgot about security.
    Or heck, how about when they take it up as a hobby and distribute some pieces here and then and maybe compile an application.
    ...And then people's computers start crashing. Wonderful.

    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.
    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. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    Didn't we go over that last time? It's not super paranoia; it's security.
    Imagine Microsoft not using all those security checks. Well, we'd have a 1000x more patches today.
    Teach newbies to write safe code and do so early. It's growing all the more important in today's world.
    You are assuming the purpose is to prepare everyone for employment at Microsoft, or some other place where scale would necessitate this kind of paranoia, because they probably have employees that will intentionally incorporate buffer overrun bugs on a bad day, so the move has been more and more toward "foolproof" languages to help management manage this not doubt insidious and significant problem.

    However, while this may be true for many, it's not true for everyone. I have no desire to work anywhere like that -- I'd rather just do unskilled labour, in fact. So your perspective is valid, but it should never never be taken as a universal rule (eg, so that we start adapting the C standard to it*). Programming as business concerns are not synonymous with pure programming concerns, where I think freedom is more important.

    The fact that some software houses have problems with people shooting each other in the foot is not my problem. Let them use Java or something, or spend money on mem checkers (which I believe is a cost of doing such business).

    * however, I could care less if they ban gets(), since it is silly and redundant.
    Last edited by MK27; 06-11-2010 at 10:25 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

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it is absolutely necessary. Microsoft don't use C# for the kernel because it just doesn't work. They are stuck with C/C++. That means they need safe facilities.
    Similarly, we see a lot of C programmers here developing in C for Windows. For basic apps. Yes, for basic apps. Should we force them to go C++/Java/C#, then? Because there are no safe facilities in C.

    Anyway, the thing is, at Microsoft, they have a lot of security guidelines and stuff. Making sure there are no buffer overruns is just one of them. And to be fair, buffer overruns is a huge problem these days, so yes, I completely believe that it should be mandatory for everyone, as a rule, to use safe facilities.

    Unsafe use of memcpy/strcpy/strcat is just as bad as gets.
    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.

  10. #25
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    No, it is absolutely necessary. Microsoft don't use C# for the kernel because it just doesn't work. ...the thing is, at Microsoft, they have a lot of security guidelines and stuff.
    Terrific! BUT MICROSOFT IS NOT ANSI OR THE ISO.

    Unsafe use of memcpy/strcpy/strcat is just as bad as gets.
    Okay, so in house MS can ban the use of these functions. Or use strict C++ or Java or whatever. The rest of use will just have to toughen up and learn to use your brain whilst coding, because all three of those functions are perfectly safe when used properly.
    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

  11. #26
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Microsoft to banish memcpy
    next time, Assignment operator ?maybe

  12. #27
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bayint Naung View Post
    Microsoft to banish memcpy
    next time, Assignment operator ?maybe
    See, problem solved! I don't think cboard has to do their work for them. Bill has $54B, let him pay someone for that tish.
    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

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MK27 View Post
    Terrific! BUT MICROSOFT IS NOT ANSI OR THE ISO.

    Okay, so in house MS can ban the use of these functions. Or use strict C++ or Java or whatever. The rest of use will just have to toughen up and learn to use your brain whilst coding, because all three of those functions are perfectly safe when used properly.
    I'm not saying to use Microsoft functions. I'm saying to create your own safe functions that add security checks so that you don't commit to these mistakes.
    That's why I said it would be nice is safe alternatives were added to the language (in the standard, of course).

    Quote Originally Posted by Bayint Naung View Post
    Microsoft to banish memcpy
    next time, Assignment operator ?maybe
    They're only banning it in their internal development. This doesn't affect the rest of the world's C programmers.
    (Though they should take a hint and consider security.)
    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. #29
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    I'm not saying to use Microsoft functions. I'm saying to create your own safe functions that add security checks so that you don't commit to these mistakes.
    That's why I said it would be nice is safe alternatives were added to the language (in the standard, of course).
    This is completely unnecessary. 95% of memcpy calls can be done without checks simply by setting up the input and output correctly.

    Please do not say "oh but what if I do this":
    Code:
    char buffer[16];
    memcpy(buffer, something_else, 1000000);
    That is JUST STUPID. TOO BAD. YOU LOSE. LEARN TO PROGRAM.

    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.

    Since it won't be, they can just make C# faster or something. Won't happen if half the underlying code is totally redundant safety checks, of course.
    Last edited by MK27; 06-11-2010 at 10:50 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

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MK27 View Post
    This is completely unnecessary. 95% of memcpy calls can be done without checks simply by setting up the input and output correctly.
    ...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.
    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