6 ms·
C is simple. But not ergonomic. Or safe. It's bad to the point of being almost broken. "Oh but it's powerful", yes, a chainsaw is powerful but even that has an
by raverbashing 7y ago
C is simple. But not ergonomic. Or safe. It's bad to the point of being almost broken.
"Oh but it's powerful", yes, a chainsaw is powerful but even that has an emergency brake. C has none of that.
(Not to mention compilers that take "undefined behaviour" as a code word for "bamboozle time")
- jacquesm 7y agoThe C emergency brake is called valgrind.
- saagarjha 7y agoOr some of the newer sanitizers.
- pjmlp 7y agoExcept it doesn't break on all platforms. So I have to hope for the best when some piece of hardware, coded in C, gets plugged into the network. Morris Worm is 30+ year old, and yet best we can do is having platforms like CHERI, Solaris SPARC ADI, ARM MTE, which also happen to be quite specific.
- jacquesm 7y agoC has a bad rep, and deservedly so in the context of anything related to security. We have not been able to sanitize the language because the ability to surprise the writer of some bit of code is almost a core concept in the language. This sucks. But at the same time the way newer languages are held up as the manna from heaven is rather tiresome as well. By the time they will be deployed in the quantities that C code is today there will be exploits in those languages because it is mostly the programmers that are fallible. Now, some languages - again, C - make it easier to shoot yourself in the foot than others. But I've seen plenty of code in Java that was exploitable even though memory safety in Java is arguably at a higher level than say Rust. So you'll be hoping for the best for some time to come, no matter what the stuff you install was written in.
- msla 7y agoIt is possible to foreclose on some kinds of error entirely, and if you compare like-for-like, those benefits become obvious. If, on the other hand, you take the productivity gains of a higher-level language as a reason to write more ambitious programs, well, new functionality can find new problems.
- pjmlp 7y agoIt all boils down to: Σ UB + Σ memory_corruption + Σ logical_errors ≥ Σ logical_errors
- jacquesm 7y agoVery true. But every language that can reach the hardware will have the ability to wreck things in spectacular and hard to predict ways. Every new language ever was touted as the one that would finally solve all our problems. For Java and COBOL the historical record borders on the comical. I have no doubt that the same will go for every other language that we just haven't found the warts in yet. Two steps forward, one step back, that seems to be the way of the world in the programming kingdom.
- klingonopera 7y agoVery nice, but I'd nitpick memory corruption and expand it to hardware errors, or add that category. Sending digital signals over analog medium (read: always) can fail, and may do so regularly depending on hardware itself and environment conditions. Simple examples: Digital 3.3V signals sent over 10m, high-capacity lines (lose a bit here and then), or an overclocked CPU undervolting just at the right time, etc... EDIT: I'd also argue that UB is a type of logical_error. Not the fact that UB exists within the language, but in that the logic failed to account for the scenario?
- pjmlp 7y agoExcept that unless we are talking about hardware design languages, no programming language accounts for hardware errors as such. UB is its own kind of error, specially since ISO C documents over 200 use cases, and unless you are using static analysers, most likely won't be able to find out that are failing into such scenarios as no human is able to know all of them by heart, whereas logical errors are relatively easy to track down, even by code review.
- varjag 7y agoNearly every piece of networked hardware has critical software running written in C, and the consequences are not nearly as disastrous as reading HN would make us believe.
- pjmlp 7y agoThe CVE database proves otherwise. Unfortunately liability is not yet a thing across the industry.
- rightbyte 7y agoWhat does that site prove? https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=stack https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=stack 3496 entries https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=pointer https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=pointer 2389 entries https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=java https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=java 2034 entries A possible outcome is that you would trade pointer bugs etc. for "Java bugs" if Java embedded were used everywhere. Embedding a complete runtime increases the attack surface alot.
- pjmlp 7y agoContrary to urban myths, C has a runtime as well. Many of those Java exploits are on C and C++ written code layer, yet another reason to get rid of them in security critical code. According to Microsoft Security Research Center and Google's driven Linux Kernel Self Protection project, the industry losses due to memory corruptions in C written software goes up to billions of dollars per year. Several reports are available.
- jstimpfle 7y ago> Contrary to urban myths, C has a runtime as well. Would you like to tell us how it compares to e.g. Java's runtime as well?
- 7y ago
- andrepd 7y agoUndefined behaviour is a tool to generate faster code, by shifting some burden of correctness to the programmer. For example, accessing past the array size is undefined behaviour. This lets the compiler skip the bounds check, an expensive check+branch on each array access. The trade-off is that now the programmer has to ensure correctness manually (or use an alternative interface that wraps a bounds check) else bad (TM) things will happen.
- AaronFriel 7y agoDespite this, tools will often not warn of undefined behavior or these premature optimizations. Quite the opposite, the compilers are almost user hostile for trying to ensure safety and correctness. I cannot see into the minds of the GCC and LLVM teams responsible for the C frontends, but to the extent that you'd think the compiler should do anything here, they seem to think it's largely not their job to improve safety or correctness. That's the job of static analysis tools (which can cost a lot of money), runtime analysis tools (which can cost a lot of time in writing tests that exercise all code paths), and fuzzers (yet more time). Punting all of this extremely important reasoning to "the programmer" is ridiculous when it's been proven over and over again that even incredibly smart C programmers get this wrong.
- saagarjha 7y agoCompilers could certainly do a better job when performing optimizations that “abuse” undefined behavior, but in general this isn’t a trivial problem. The way optimization passes are set up can often make it difficult to generate warnings that have a low false-positive rate.
- AaronFriel 7y agoDo you think it might be a problem that undefined behavior optimizations and/or pattern matches are so prolific in C that reducing the "false-positive rate" of ... actually bad undefined behavior (?) would be the hard thing? Why not disallow undefined behavior and instead have the compiler emit: "Hey, this branch is never taken, if you want us not to compile it remove the dead code". Seems like that's exactly the same sort of putting the onus on the programmer that andrepd mentioned, but without the side effect of millions of device drivers are zero day remote code execution vulnerabilities waiting to happen?