23

Old question C's “bad” functions vs. their “good” alternatives is in a poor condition.

I have mixed feelings about the question itself. I can see it working as single answer wiki, but it still feels too broad.

Selected answer is the worst part. It's a blanket statement with list of things, some of which are just incorrect.

Rest of answers are not much better: list of random things or rants. There is some correct info there, but I don't think it's in good form to be useful to someone.

Can we fix this question, or should we just close or delete it?

Clarification:

I didn't mean to single out the question only. Question is still, in my opinion, too broad after the edits, but answers are my main concern: They are not complete list of problematic functions, and some listed alternatives are definitely not ok. Many answers also don't explain what the problem with the function is.

My main problem with the question and its answers at the moment is this: In its currect form it's not reliable learning resource to beginners finding it.

14
  • 3
    Deleting this question is not an option, the information is still valid and can come to use, we can even edit the question and the answers
    – weegee
    May 20, 2019 at 8:25
  • 12
    It's a question from 2009 ,about 50% of it has already been turned into a community wiki (never a good sign, IMO). Slap a historic lock on it and just let it be?
    – Gimby
    May 20, 2019 at 8:32
  • I rewrote the question, do you think it's better? I'm going through the answers now to collect the good information and resources May 20, 2019 at 13:27
  • 2
    @CarsonMyers arguably "Is there a list of functions" is asking for off-site resources. But it does fit with the pre-existing answers which provide exactly that.
    – Gimby
    May 20, 2019 at 13:51
  • 3
    @Gimby good point, and although there were some offsite resource posted as comments, my ambition is to move that information to StackOverflow (since some of them have disappeared already) May 20, 2019 at 13:54
  • @CarsonMyers that sounds like an improvement... but also something that requires active maintenance as it's a language that does not stand still.
    – Gimby
    May 20, 2019 at 13:58
  • @Gimby I am definitely not an expert on changes to libc over the years, I will seek out some more information about that before deciding whether its a pointless endeavour. The handful of popular examples in the answers have remained true over the past ten years at least, though May 20, 2019 at 14:05
  • I see nothing wrong with the question, especially after its latest edits. It sounds like your primary complaint is with the answers. If so, you can fix that easily: post a better answer. May 20, 2019 at 20:28
  • 1
    @CodyGray Yes, my primary complaint concers the quality of the answers, but also the question. I tried to clarify my question. And I disagree with your solution: There is already 14 answers, me posting yet another is not the solution.
    – user694733
    May 21, 2019 at 7:35
  • @user694733 would it be inappropriate for me to start a CW answer and change it to the accepted answer? May 21, 2019 at 8:56
  • or to substantially augment the already accepted answer with extra information? May 21, 2019 at 8:59
  • @CarsonMyers That is the dilemma I had: I would like to see accepted answer changed drasticly (or removed), but I don't know what is the correct way to do that. I don't think it's accepted to completely rewrite entire answer under someone elses name.
    – user694733
    May 21, 2019 at 10:00
  • @Gimby one could argue that basically every answer in SO requires active maintenance, since most languages also do not stand still, and most solutions to programming problems will change, given enough time. May 21, 2019 at 17:17
  • If you are going to single-handedly close a question being discussed as a mod, as @SamuelLiew did, then I would strongly suggest you post an answer here explaining why.
    – Travis J
    May 21, 2019 at 20:30

2 Answers 2

31

I wrote the question originally, and looking back on it almost 10 years later I can definitely see the issues with it.

It was motivated by the fact that when trying to learn more C I kept running into individual functions that were recommended never to be used, and hoped I could gather a more complete list or a pattern of standard library naming I could draw on to not make such mistakes. IMO that kind of information could still be useful and it seems like enough information landed in the answers to salvage something from it.

I could add some kind of definition of the dangerous functions (not thread-safe, not bounds checked, not null-terminating, etc.) in the question and then source some information from the answers to add a wiki answer with a small list of functions, their properties, and alternatives. Someone in the comments linked a Microsoft banned functions list that looks useful and while not all of the answers are accurate, certain functions appear in them often enough to suggest they're worth being wary of.

1
  • The question better now. I still think it's a bit too broad, but I don't think much more can be done with it without invalidating the existing answers. I think that at this point we should focus our attention on getting rid of the worst answers somehow.
    – user694733
    May 21, 2019 at 7:44
1

It's clear what the OP wanted: a list of standard C functions that are "dangerous" to use for the unwary.

It's however unclear what exactly qualifies as "dangerous", so it's unclear what an answer should and shouldn't include:

  • Bounds checking?
  • Invalid/untrusted input?
  • Irresponsible buffer use?
  • etc?

The answers reflect this unclarity: their topics are all over the place.

So the question should be closed and possibly deleted as unclear. "Too broad" would also be fine because including all of the above clearly wouldn't fit into a few paragraphs.

16
  • I took another swing at the wording of the question; it'll be more difficult to find a solution for the disorganized answers but I'm willing to write one/edit the accepted answer to collect the good information together May 20, 2019 at 13:29
  • 1
    @CarsonMyers we cannot "collect the good information together" because it's impossible to decide which information is "good". May 20, 2019 at 13:30
  • 7
    Why do you think so? From re-examining the answers I see that there are clear patterns in the standard library which are considered unsafe or require a lot of extra care (esp. by beginners) - some of the answers have clear and justified information and/or links to quality resources. Surely that can be cleaned up and work well as a CW answer May 20, 2019 at 13:37
  • 1
    "there are clear patterns in the standard library which are considered unsafe" - I don't see any clear pattern. Non-"n" functions? - There are several comments for the accepted answer pointed to the non-bad functions in the list. Non-reentrancy? Who think about reentrancy in a purely single-threaded application? You care about beginners, but wrong answers affects them more than a programmer with a medium background. Moreover, a replacement for any of the listed "bad" function seems for me to be more difficult for a beginner than a "bad" function itself.
    – Tsyvarev
    May 21, 2019 at 15:18
  • 1
    Preparing canonical list of bad functions could be a holywar like, e.g., C vs. C++.
    – Tsyvarev
    May 21, 2019 at 15:21
  • 1
    @Tsyvarev there's no "holy war" between C and C++. Some programmers prefer the former, some prefer the latter, some (I would guess most, actually) even use both, and that's it. It's like saying there's some "holy war" between potatoes and carrots. In my opinion, a canonical list of "bad" functions could work, if every entry in the list is backed by research: There should be a respectable source explaining why the function is bad, and what should be used instead. There can be sources that diverge in this, and these entries should be simply marked as controversial or something like that. May 21, 2019 at 16:04
  • Also, the patterns that Carson probably is talking about are: Functions that change the passed parameters in unexpected ways; Functions that have weak guarantees; Functions that don't enforce preconditions; Functions that don't signal errors (or signal them badly); Functions that corrupt data or state when an error happens; etc. I got this just from reading the first few answers. May 21, 2019 at 16:10
  • Finally (Sorry for the long comments, BTW), I agree with you that wrong answers are specially bad for beginners: That's why we should clean them up. May 21, 2019 at 16:12
  • @CássioRenan: From my point of view, all these categories (patterns) are not denote bad functions. They are just "care when use this function" note. Actually, I agree with that answer, which states that only gets is bad (because its crashing on wrong user input couldn't be avoided). Prohibition of any other non-deprecated function is based only on the programming style, following one of which is mostly subjective (or depends on the project). And any of that function could perfectly work if follow some other coding style.
    – Tsyvarev
    May 21, 2019 at 17:10
  • @Tsyvarev that's it! The problem is not "dangerous functions", the problem is dangerous practices. Some functions make it easier to shoot yourself in the foot, some harder, but the lack of pitfalls knowledge is the ultimate problem. May 21, 2019 at 17:27
  • @ivan_pozdeev no, those are two separate problems. They are not mutually exclusive. If I write a function that behaves badly, it's still a bad function, no matter what good practices you apply when using it. Tsyvarev points out gets as one such function. May 21, 2019 at 17:32
  • @Tsyvarev the problem with these functions is that any amount of "care" you take when using them basically amounts to adding boilerplate code on every call, to treat issues that the function could have taken care of itself. That's why many of them have better alternatives whose only difference is that they provide better guarantees, preconditions, error checking, etc. Or else, why would such alternatives even exist? May 21, 2019 at 17:39
  • @CássioRenan but C is not designed to "behave well", it's rather designed to be implemented well. It's "a better assembler", all its constructs and operations have direct machine code equivalents (optimizations aside). "Guarantees, preconditions, error checking" add more nontrivial logic that adds overhead. It's a fundamental judgement call of how much or little coaching a language can provide and still be considered "useful". May 21, 2019 at 17:55
  • @ivan_pozdeev You're mixing the design of the language with the design of the standard library. Those are two very different beasts. Even the ISO C comittee is publishing technical reports (TR 24731-1; TR 24731-2) that indicate that the library is moving toward safer functions. I agree with you that this is mostly a judgment call, but the (obviously not unanimous) consensus is that safer functions are better than unsafe ones. May 21, 2019 at 18:29
  • And I assert again that the "overhead" that those safer functions add, would probably need to be added anyway by the programmer, should he still choose to use the less safe versions. Only now there is more boilerplate code, for no benefit. Not only that, but also on the safer ones, such checking can be optimized away, if the compiler can prove the assertions always have the same result. May 21, 2019 at 18:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .