10 ms·
I cannot consistently write safe C/C++ code
- wfunction 9y ago> I cannot consistently write safe C/C++ code. I'm not sure how to interpret what this means. What do "consistent" and "safe" mean? Is safety about not corrupting program data? Even when dealing with Python arrays, I can end up corrupting my arrays one way or another (off by one, race conditions, etc.). Is "consistent" about going days without a bug? Because I can't do that in any language. If not, what do these mean? It'd be really nice to know what other languages he can write safe code in, so we can have something to compare to.
- cbanek 9y ago> Is "consistent" about going days without a bug? Because I can't do that in any language. Amen to that. Even when I feel like I cover most of the cases, the sands of time will slowly eat away at the foundation, things become deprecated, and new error conditions added all the time.
- danieldk 9y agoI think it is clear what he means from the remainder of the blog post: I see a lot of people assert that safety issues (leading to exploitable bugs) It is obvious that he is referring to typical C/C++ safety issues buffer overflows, use after free, etc. Even when dealing with Python arrays You can introduce security vulnerabilities in any programming language. But safe languages exclude a host of memory-related vulnerabilities, which are a substantial proportion of all vulnerabilities. Safety is not binary, some languages provide better safety or better means to model domain data safely than other languages.
- continuational 9y agoConsistent as in you can do it while focusing on solving business problems, instead of only when safety is the primary thing you're concentrating on. Safe as in a minor bug is unlikely to be an exploitable security hole.
- simias 9y agoYou can have "safe" buggy code as long as you avoid the subset of bugs that can open a security vulnerability. It's possible to make such a mistake in any language but some make it harder than others. C's laissez-faire approach to memory management makes it very easy to introduce a small bug that leads to a major security vulnerability. Indexing out of bounds in python throws an exception. Indexing out of bounds in C triggers the dreaded "undefined behaviour". Here be dragons.
- pjmlp 9y agoOf course every programming language is not perfect, including its library and related implementations. In all of them is possible to introduce logical bugs. The problem with C and its derived languages is that not only one has the logical bugs common to all programming languages, there are the memory corruption and UB introduced bugs to worry about as well. While one can think as being super competent, make use of all tools to reduce such error cases, there are always situations where such errors get introduced due to fatigue, project pressure, continuous interrupts. Additionally since most of us don't work alone, the actual code quality is an average of everyone that has ever worked on the code, and not everyone shares the same goals regarding quality of their work. I share the feeling with the author, always tried to follow C and C++ best practices, when coding in C I adopted or evangelized tooling, safety standards or known books that lead to safer code. Yet, like everyone else I had my share of memory corruption issues back on my C and C++ days. One anecdote was trying to find out a memory leak that was bringing down a server in production, with the customer calling technical support every single day. It took one week to track it down, and it wasn't something I would advise anyone to experiment.
- danieltillett 9y agoThe good thing about C is there is an amazing tool support that can make it as safe as any other language. The bad thing about C is that most developers don’t know about (or don’t use) all the tools.
- clarry 9y agoThey don't even use their compiler.. case in point: https://news.ycombinator.com/item?id=14787072 https://news.ycombinator.com/item?id=14787072 Of course they always just blame the language. Like the language needs to be responsible for the implementation and its proper use.
- kbenson 9y agoAny tool that requires special knowledge to use safely rather than defaulting to safe operation and requiring special knowledge to use unsafely is poorly designed. If the design specification for that class or tools encourages or requires that, it's a poor specification indeed.
- faragon 9y agoYet 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.
- fulafel 9y agoGood risk management is about cost vs payoff analysis. To address the risk of car accident, first you stop juggling chainsaws while driving, then you start to wear seatbelts, and then you start thinking about how the remaining inevitable driving mistakes can be mitigated through technology or practices.
- posterboy 9y agoSafe subsets of C exist already. Any safeguards active at compile time could in principle be achieved via static code analysis. Only, if that means making assumptions and transformation to a different type system, that's like putting an old engine in a new car.
- faragon 9y agoSure. And the best "insurance" is having competent programmers.
- tormeh 9y agoYes, competent drivers help, but I'm not sure a competent driver juggling chainsaws is better at driving than a bad driver not juggling chainsaws.
- CJefferson 9y agoThere 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.
- simias 9y agoI 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 you're a good driver. Guns don't kill people, people do but if your gun has the bad habit of firing randomly when you don't want it to I guess it's fair to blame the gun a little bit as well.
- huhtenberg 9y ago> Real men write in assembly, maybe C if they're a bit tired. root@boxen:~# cat > a.out
- concede_pluto 9y agoLaziness, impatience, and hubris. # zcat > /dev/kmem
- Tamara99 9y agomÿ läst päÿ ċhëċk wäs $9500 wörkïnġ 12 höürs ä wëëk önlïnë. mÿ sïstërs frïënd häs bëën ävëräġïnġ 15k för mönths nöw änd shë wörks äböüt 20 höürs ä wëëk. ï ċän't bëlïëvë höw ëäsÿ ït wäs önċë ï trïëd ït öüt. thïs ïs whät ï dö, ====http://cutt.us/7N2sG http://cutt.us/7N2sG
- posterboy 9y ago> "competent coders don't make mistakes" There's a bit of folklore that I can't remember correctly. One of the seven steps to becomming a hacker (in the pejorative sense) is this kind of overconfidence, not to make mistakes. The last step is (the believe in) complete controle and omniscence. Another one, I guess, is insomnia. It hang as a poster in informatics class - fond memories.
- adambard 9y ago> 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 https://en.wikipedia.org/wiki/Ignaz_Semmelweis
- superbatfish 9y agoI don't know what the Dunning-Kruger effect is, but I can tell you for a fact I'm too smart for it to affect me.
- kbenson 9y ago> I cannot consistently write safe C/C++ code. I'm not ashamed of that; I don't know anyone else who can. I've heard maybe Daniel J. Bernstein can For anyone that thinks this, you might be interested to find that DJB himself apparently thinks C, or at least the current state of C as implemented by the common compilers, is untenable for important, long lived software.[1] At least, that's how I interpreted his position. He was responded to as you would expect, unfortunately. Credit where it's due. [2] 1: https://groups.google.com/forum/#!msg/boring-crypto/48qa1kWignU/o8GGp2K1DAAJ https://groups.google.com/forum/#!msg/boring-crypto/48qa1kWi... 2: https://news.ycombinator.com/item?id=14170585 https://news.ycombinator.com/item?id=14170585
- irundebian 9y agoNice 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.
- stinos 9y agoThere 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.
- irundebian 9y ago> as there will probably never be languages without any flaws Yes, but we can try to develop better language. Rust is such a try. And that's good. > Sure, but I don't belong to that fraction of programmers who think they write flawless code or develop a cult with old, error prone languages such as C. The important thing is that we are not discussing about programmers making mistakes, but about a language which is old and which is making mistakes easy. C proofed in sufficient cases that it belongs to the museum.
- stinos 9y agoit belongs to the museum I agree that in C it's way easier to make mistakes than in e.g. Rust, but I don't agree that (or whatever other reason) is proof that it belongs in a museum. Take microPython for instance: it's a relatively young project written in C, it allows running a pretty complete Python 3 implementation on a variety of microprocessors as well as on PC/mac/... Suppose we put C in a museum, what do we use instead to achieve the same functionality?
- Loony2 9y agoI'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.
- kbenson 9y agoThis 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.
- Loony2 9y agoI agree, but I still believe that one issue here is the lack of understanding of what the process around software development should be. We would get rid of some issues if we used a safer language, but the real issue is that we don't find the issues, the attacker does instead. So people are finding the issues, but why are not the people writing the software finding them? I believe that you should have a development team that make sure there are no issues to be found, no matter the language you are writing your application in. That means you run the same tests, no matter the language, so in the end it doesn't matter what language you write it in. And you chose a language that fits the problem, you don't make the language fit the problem. So I think your analogy of an airbag is wrong in some sense. The issue isn't weather we have an airbag or not, the issue is that we don't test if we have an airbag and then go "Whoops, the airbag didn't deploy in the crash and somebody died". We as programmers like to think of ourselves as engineers, but we don't treat the profession as engineers, we very often deploy code we know are not tested, we might even know it is buggy, you open yourself up to a lot of damage if you do that as a bridge builder (even though it has happened). I'm tired and this turned into a rant, but I hope that my point comes across. EDIT: I don't mean that we should write bug free code, I mean that we should strive for code without security issues. It can be done, I work at a place where we have written code for 15 years, not only C code, or more without any remote exploitable holes.
- millstone 9y ago> 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!
- hsjoberg 9y agoIn 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.
- millstone 9y agoThat's a great point. Often you need to run your system in simulation via a model. Generating the code directly from the model is one way to prove that your system matches the simulation. Of course the modeling tools speak mostly C. This illustrates how C's weaknesses are mitigated in practice, and how much work prospective C-replacements must do to be competitive.
- ccommsxx 9y agoThe 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.
- pjmlp 9y agoFact 1, the article doesn't mention Rust a single time. Fact 2, mostly safe systems programming languages exist since ESPOL (1961), 10 years older than C, and with a great linage of attempts of safe systems programming outside AT&T walls, so plenty of alternatives are available So as someone with more than 10 years of C and C++ experience, among other programming languages, before focusing on Java and .NET, I find these kind of articles valuable because they are the proof there isn't such thing as the 10x C developer that never does mistakes.
- ccommsxx 9y agoI feel you're missing my point. The article doesn't mention rust but my comment was phrased in the context of the current rust craze on HN. As I said, I agree with you and the author that all of us do inevitably make mistakes. I also agree that the c-family of languages makes it somewhat more easy to shoot yourself in the foot in a bad way than others. I'm not saying innovation on safe languages is bad, or that using safe languages is bad. My disagreement with you and the sibling is probably on the point of whether one can learn something useful about the limitations and pitfalls of c-family languages by reading a single-page all-opinion post about it. You and the sibling appear to believe so, I do not. I don't think we will come to a complete agreement on this, but I certainly understand and respect your and sibling's view.
- barrkel 9y agoThe more you know an a C++ programmer, the more you're aware of all the things that can bite you and all the things you need to do to make sure you don't get bitten. Writing code within that cognitive envelope is a tax that's often underestimated. After spending some time out of that world, I find it amazingly tedious to get back into it. If you're not aware of many of the pitfalls, you might not even be aware of the envelope you should be aware of.
- jhasse 9y agoHow about you start writing C++ and not C/C++ code? The two languages are so different, it isn't fair to mention them as if they were the same.
- danieltillett 9y agoC++ is certainly a different language now, but you can write code that compiles as either C or C++ with a little care. Everything I write compiles as C or C++ (yes I cast all the void allocations and choose to ignore the C warnings that this is verboten).
- Manishearth 9y agoThe author worked on Firefox, a largely C++ codebase, so he was writing C++. I assume he feels that the problems he talks of are common to both languages, if different in magnitude.
- sclangdon 9y agoIt makes me smile when someone starts off a complaint with something similar to "I've been programing in C++ for x decades. I have y qualifications that should impress you. I worked at z company which should impress you further. Yet, despite this, I cannot write safe C++ code". The author's post, in particular, is excellent as it follows this token introduction with "I don't know anyone else who can write safe C++ code, either". This is further strengthened by "people I know to be skilled programmers (based on what, I don't know), have never professed that they can write safe C++ code, and therefore people who do, are obviously suffering from some psychological effect". I mean, really? You can't do it, and your friends can't do it, so no one can do it? Even though there is a world of software out there that does exactly what you say can't be done? And further, anyone who says they can do it must be less-skilled and suffering for a form of illusory superiority complex? Get over yourself. How ironic that he knows about the Dunning-Kruger effect, but fails to apply it to himself.
- shusson 9y ago> Get over yourself The guy is simply describing his personal experience. Why don't you describe your own personal experience instead of attacking him?
- nec4b 9y agoI wonder how can anyone even use computers these days, since C and C++ are so unsafe. My OS is written in C and C++, also my browser and my word processor, my shell and most of the applications that I daily use. The firmware in my phone, my set-up box and my car is also written in those two languages. Also all the major web servers are written in those to languages. Maybe Rust programmers are an exception, but I personally spend 99,99% of troubleshooting time (in any language) on fixing logical bugs that I make. It's hard to get excited about a language like Rust, when it seems that it's community is only interested in spreading FUD.
- clarry 9y ago"So unsafe." The thing is, we (C programmers) are a careful bunch and we like to treat everything as a potential security issue. Even if the likelihood of exploitation on a real world system is literally nil. People see these fixes and possibly associated security advisories, and they think it's all exploitable. Then one actually exploitable bug from some 15-year-old code gets publicized and the publicity spreads everywhere. Look, another exploitable bug! C is sooo friggin bad and every line is an RCE waiting to happen! I think the safety issue is blown way out of proportion because of that. It doesn't help that when people discuss UB, they assume the most adversarial compiler possible that literally does all it can to turn the UB into a big problem. And that literally everyone has to be using that compiler and that they may not use the compiler flags that turn off the bad behavior.
- pjmlp 9y agoBefore 1990, other than UNIX related software, that was hardly the case.
- fizixer 9y ago> ... for over ten years my main job was developing and reviewing C++ code ... > I cannot consistently write safe C ... When you learn the difference and have spent over ten years on a C-only project and still have the same opinion, I'll take you a bit more seriously.
- alok99 9y agoI'd be interested in trying an online coding exercise like the one he mentioned. I've definitely fallen into the trap of thinking that my code was robust because it worked for my specific use case (and no one tried to break it). Now I feel much more skeptical that my code works at all.
- Sir_Cmpwn 9y agoI really wish we would disconnect C and C++. They're different langauges used entirely differently by different people, and C++ gives C a bad rep.
- nikbackm 9y agoC is more than capable of getting a bad rep on its own.
- amelius 9y agoIsn't this the same as saying you can't consistently write bug-free code? And isn't this sort of the holy grail of CS?
- kbenson 9y agoYou are conflating "safe" with "bug-free", when safety has a specific meaning in this context, which is memory model safety. Safe code eliminates a class of errors, and thus reduces the possible bugs your program may exhibit. At this point we can eliminate certain classes of bugs, and sometimes with no runtime cost. The usual example used is Rust, where extra information supplied in the source is used to ensure certain bugs are not possible at compile time. It requires some extra work, but I think likely quite a bit less than the more extreme coding standards used to produce safe C code, but those may cover a different or larger subset of bugs.
- jokoon 9y agoSafety, speed, language convenience. Pick 2. Safety is an issue everywhere you are programming something, good programmers just know how to mitigate it. And to be really honest, you don't always need to use C++. Just use it where performance is needed, write good code, review it, etc, and when you need to go at a higher level, use python or any other scripting languages. C++ should be used to write libraries. There are not so many softwares in the world that need to be entirely written in C++, and if they do, more time is spent writing and fixing such software. "There are languages people complain about, and languages nobody use". I don't dislike rust, but when I read its syntax, it doesn't seem simple enough to learn, read and write, the learning curve seems pretty steep, so it won't really do well with students.