5

C often seems to be taught in terms of contracted examples without much regard to code safety, such as buffer overflows etc. I have often wondered if there are proven, totally safe ways of writing C code, because I have seldom encountered C written to handle all eventualities correctly. And now, reading that even the best Gnome programmers make exactly these same errors in C, I think it is time to ask this question here.

In the porting work Mena noticed a number of actually usual errors in the C code such as overflows, which unfortunately are not always obvious. The code for some of these errors comes from the Mena's best C programmers, the Gnome Project ever had. This does not mean that the project has no good programmers. The reason for these mistakes is not people, but rather the language C itself, which is simply "hostile".

So, is it even possible to write safe C? Are there resources which explain how to avoid all these pitfalls?

14
  • To the downvoter - Care to explain how this is not on topic, being about "software development methods and practices"? Jul 31, 2017 at 17:12
  • 2
    It's probably being down voted because it's "too broad" (it seems to be looking for a discussion on writing safe C, not asking a specific, answerable question) or a resource request on material to write safe C (where resource requests are off-topic here).
    – Thomas Owens
    Jul 31, 2017 at 17:24
  • True, it is deliberately a bit broader than "How to avoid buffer overflows with scanf", maybe I should narrow it down to "What guidelines exist to avoid buffer overflows at all times". Jul 31, 2017 at 17:31
  • 1
    Even that proposed edit is too broad. Questions shouldn't generate lists of things or discussions about a topic. They should generate a handful of answers, which can be judged (voted on by) the asker (you) and the community based on the contents of the question.
    – Thomas Owens
    Jul 31, 2017 at 17:33
  • 5
    @mike yes we would. We could learn them from reading whole books on the subject. We wouldn't benefit from the rats nest of partial list answers that would get up-voted for reasons haven't very little to do with their comparative merits. It's not you. It's us. Jul 31, 2017 at 18:11

1 Answer 1

4

Developed by the Motor Industry Software Reliability Association (MISRA), the MISRA C guidelines specifically contemplate safe and reliable use of the C programming language in embedded systems.

2
  • The "criticism" section of that Wikipedia article is not very complimentary. Jul 31, 2017 at 20:24
  • @RobertHarvey Automated testers to check MISRA compliance can be pretty poor, flagging up loads of false alarms. Trying to re-write existing software to meet MISRA rules will probably introduce more bugs than it removes. Neither really indicates a problem with MISRA-C itself.
    – Simon B
    Aug 1, 2017 at 10:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.