Live data from Hacker News

I cannot consistently write safe C/C++ code

robert.ocallahan.org

21–30 of 123 posts

Re: I cannot consistently write safe C/C++ code

#21
I've worked in "IT security" as a C programmer for about 10 years. I both agree and disagree with this article.

A competent C/C++ programmer will have a lot less of problems like buffer overflows and crap like that, I don't think a buffer overflow has been found in any code I've written during my 10 years as a C programmer.

I have still written code that has security issues though, most of them stem from poorly designed code and are not necessarily a language problem.

I'm not claiming to be a super human here, I've had my fair share of gotchas, like off by one errors and issues with pointer arithmetic when refactoring code and so on and we might actually reduce the time needed to verify that C code is safe if we change to another "safer" language, but I'm 100 % sure that you still have the issue with poorly designed code even with a "safe" language. And from my experience it's a lot harder to find those problems, since you need to understand how the code base works and how it fits together to find those issues.

Re: I cannot consistently write safe C/C++ code

#22
post #17
post #3

Yet another subtle pro-Rust rant against C. Programming is hard, and writing safe code requires knowledge, not just in C, but in every single language. Even in formal-validable languages you can make mistakes: may be you'll not make errors by using sprintf, but can make others because of using a more complex language.

Rust does not appear in that page even once.

Reading between the lines, since he mentions Mozilla Rust is likely accountable for this post in some manner (even if loosely). That said, calling out this post as a pro-Rust rant against C is completely ignoring the fact that it might also be right, and origin is irrelevant.

Positions should be evaluated based on their merits, not their origins.

Re: I cannot consistently write safe C/C++ code

#23

Nice post. There are still to many C/C++ wannabes who think C is an awesome language and their are awesome hardcore programmers. If they are challenged to code something, it's highly probable that flaws will come up.

There are still to many C/C++ wannabes

There are still too many people using the term C/C++ :] It never really was a thing, even in the beginning (I mean, just take e.g. destruction at scope exit: that alone makes it a very different language) and now even less with the new standards.

Anyway: a language can be considered awesome by people, despite it's flaws. Always has been, never will change, as there will probably never be languages without any flaws. (e.g. I think Python is awesome, others don't ever cease summing up it's flaws, I couldn't care less as it gets stuff done in particular situations). Just like it's inevitable every single programmer, including you, writes flawed code from time to time, challenged or not.

Re: I cannot consistently write safe C/C++ code

#24
post #5

I think this whole "competent coders don't make mistakes" is a testament to the immaturity of the software development world. Real men write in assembly, maybe C if they're a bit tired. It would be like a construction worker saying "only noobs need a hard hat" or a surgeon refusing to wash their hands because they're careful never to touch anything contaminated. Or maybe simply refusing to wear your seatbelt because…

> or a surgeon refusing to wash their hands because they're careful never to touch anything contaminated.

Fun fact, although perhaps this is specifically what you had in mind: This was pretty much the case until Ignaz Semmelweis [1] noticed that washing your hands caused patients to die less frequently. His suggestion was not well-received by the establishment:

> "Some doctors, for instance, were offended at the suggestion that they should wash their hands, feeling that their social status as gentlemen was inconsistent with the idea that their hands could be unclean."

I'd apologize for going off on a tangent if it wasn't such an accurate analogy.

[1]: https://en.wikipedia.org/wiki/Ignaz_Semmelweis

Re: I cannot consistently write safe C/C++ code

#25
> I cannot consistently write safe C/C++ code. I'm not ashamed of that; I don't know anyone else who can.

With respect, two mistaken beliefs:

1: Only a few programmers can write safe code.

2: One will naturally encounter such programmers in the course of a prestigious career working for a high-profile web browser company.

But mediocre programmers consistently write safe C/C++ code, every day. They do it in the context of aviation, automotive, etc. They do it as part of a much larger safety process, that is designed to be robust against faults at all levels, from design through development through manufacturing through end-user servicing. Software development is but one facet, and compliance to MISRA, ISO 26262, etc is only the start.

Get to know them, and see how safe C/C++ code is actually written!

Re: I cannot consistently write safe C/C++ code

#26
post #21

I've worked in "IT security" as a C programmer for about 10 years. I both agree and disagree with this article. A competent C/C++ programmer will have a lot less of problems like buffer overflows and crap like that, I don't think a buffer overflow has been found in any code I've written during my 10 years as a C programmer. I have still written code that has security issues though, most of them stem from poorly desig…

This is a rebuttal against "use a safe language and all your security problems go away entirely" but that is not generally the argument being advanced. The argument that is generally advanced is "use a safe language and some of your security problems go away entirely".

Put another way, people are arguing for airbags to become much more common, and your rebuttal is "I've gotten into some accidents, and I've gotten hurt in ways an airbag would not have helped". That's entirely possible, but irrelevant to the argument at hand (unless you also state that you never get into accidents where and airbag would help)..

Edit: Stating your position as a rebuttal may have been overstating it a bit. It's entirely possible you're just attempting to add information to the argument, in which case please read my comment as attempting to do the same.

Re: I cannot consistently write safe C/C++ code

#27
The worst thing about these kinds of articles is the troves of junior programmers that never touched systems programming with a stick before but will read this on hackernews today and sit in the office tomorrow lecturing seasoned coders how they´re dumb for not having seen the light and using an unsafe language.

This is how stupid cargo cult gets made, guys. It's easy to repeat some talking points that you found on the rust homepage or another internet forum, but doing that does not make you an enlightened programmer!

Do c/c++ for 10 years, like the author, and then you're qualified to comment about the topic. If you're new to this, try to put your time into actually learning about these topics and not just blindly repeating other peoples opinions. Please don't say things like "c/c++ is unsafe and should not be used" without understanding it first. And try to consider for a moment that a humongous part of the critical software in the world is written in it. It is the industry standard for all things embedded and low level. Consider that maybe the reason for that is not that everybody outside of hacker news is stupid.

The thing is, I'm not even saying the author is wrong (I don't think he is). I'm just saying that circulating these kinds of opinion posts here and then applauding each other for being such brilliant rust fanboys is not helpful.

Re: I cannot consistently write safe C/C++ code

#28

> I cannot consistently write safe C/C++ code. I'm not ashamed of that; I don't know anyone else who can. With respect, two mistaken beliefs: 1: Only a few programmers can write safe code. 2: One will naturally encounter such programmers in the course of a prestigious career working for a high-profile web browser company. But mediocre programmers consistently write safe C/C++ code, every day. They do it in the contex…

In automotive, code is very rarely written it is mostly generated from simulink models. But yes software for aviation automotive, nuclear and medicine equipment are running safe C code.

It is mostly generated from a model not necessarily simulink. The generated code usually have rules for the code so no global parameters, no pointers etc can be used.

Re: I cannot consistently write safe C/C++ code

#29

> I cannot consistently write safe C/C++ code. I'm not ashamed of that; I don't know anyone else who can. With respect, two mistaken beliefs: 1: Only a few programmers can write safe code. 2: One will naturally encounter such programmers in the course of a prestigious career working for a high-profile web browser company. But mediocre programmers consistently write safe C/C++ code, every day. They do it in the contex…

I doubt writing safe C/C++ code is as straightforward as the Parent post claims, otherwise people at Microsoft, Google and Mozilla must clearly be doing something wrong. They have the budget, the skills and the motivation to always produce safe code, yet their products all contain serious bugs, which would not occur if all their C/C++ code was "safe".

Re: I cannot consistently write safe C/C++ code

#30
post #10

Earlier quoted context omitted.

Sure. And the best "insurance" is having competent programmers.

There seems to be no major C system not infested with errors most other languages forbid by design. The question is, do those other languages introduce their own types of errors? In my experience, no. Do they fix all problems? Of course not. Neither does a hard hat.

Do you know any big code base without bugs? That's not exclusive of C, e.g. check any big Java code base. Regarding Rust, I would like to see the kind od bugs that would arise in big code bases (thread contention, performance issues, etc.).
Post reply on HN