4 ms·
The C Standard charter was updated, now with security principles as well
- davikr 2y ago> Undefined behaviors, unspecified behaviors, implementation-defined behaviors, and other portability issues enumerated in Annex J of the Standard should be eliminated or reduced.
- dwheeler 2y agoI appreciate the direct nod to security. But as written I doubt it's enough. The goal is to "enable" secure programming, and C already meets that goal since it is theoretically possible. The problem is that busy human developers are not good at being perfect. There are far more ways to get things wrong, and minor mistakes become far more serious problems in C and C++ compared to other languages. I do wish the group good luck!!
- flohofwoe 2y agoEven just the stated goal to 'eliminate or reduce' undefined, unspecified and implementation defined behaviour is worth a lot since it would eliminate a pretty big minefield of "unintended consequences" in modern compilers.
- jmclnx 2y agoI hope they follow that guideline, looks good to me. But being a cynic, in the past when I see good guidelines posted, it is not long before all the exact opposite happens and the guidelines are meaningless :(
- bdw5204 2y agoThere's an easy way to eliminate unspecified behavior in C or C++: Have a compiler flag to always issue warnings for it and treat those warnings as errors. That doesn't require any language-level changes to implement.
- throwawaymaths 2y agoComplete eradication should never be a goal though. There are extremely reasonable forms of undefined behavior that the compiler can use to optimize your code. It just has to be known that tripping those guardrails is undefined.
- jcranmer 2y agoConsidering that unspecified behavior includes whether or not you execute the LHS of a binary operator before the RHS, such a mode would not be very useful. Even assuming you meant undefined behavior instead of unspecified behavior, it's still not a particularly practical prospect, for you basically prohibit any use of pointers (or memory access in general) whatsoever.
- bluGill 2y agoThat isn't possible. Does "X / Y" divide by 0? Often the compiler has no way of knowing. Sure code can always check for Y == 0 - but if the programmer has knowledge the compiler doesn't that Y can never be 0 this if statement can needlessly slow down code (CPU branch mispredictions are costly). There are other places where we can prove that code is safe in most conditions but proving the exception never happens is equivalent to proving the halting problem.
- CoastalCoder 2y agoIs there a term for predicates that are checked by static analysis when possible, but relegated to runtime checks when not?
- account42 2y agoThat's just any runtime check with an optimizing compiler - conditionals that the compiler can prove are always true / always false can get eliminated.
- UncleMeat 2y agoExhaustive runtime checks for all forms of UB are basically impossible. A data race is UB. Are you going to find a way to insert correct data race detection on all writes? Writing through an invalid pointer alias is UB. How are you going to detect when that happens at runtime? Sanitizers exist and do detect a subset of UB via runtime checks, but there is nothing resembling a system that can detect all UB. That would basically require an entire VM implementing the C-standard with a truly outrageous amount of runtime overhead.
- bluGill 2y agoWe will see how this plays out. I'm not sure what options C has though, while remaining backward compatible. They removed gets (which cannot be used safely) in C11, but most things people don't like in C it is really hard to figure out how to make an alternative while still being C (that is compatible and looks like existing C) C++ is actively working on profiles, and if you enable the right profile many of the easy ways to be insecure are not longer possible. Or at least that is the goal only time will tell if they make it in, and then more time if they are both used and make a difference. However C++ developers have long said "No naked new/delete", data structures where the invariant (length...) is enforced - thus a profile can remove new/delete and the places where we now know the data structure invariant isn't enforced while still being a usable language. Because of the above anyone who is in C++ objects to phrases like "C and C++ or C/C++" - C++ is trying to leave C behind expect where compatibility with C is required.
- tialaramex 2y ago> C++ is trying to leave C behind expect where compatibility with C is required. What was it Dua Lipa sings? "If you're under him, you ain't getting over him". C is foundational to C++. Look at your miserable type system. Do you think anybody who designs type systems built that? No. This is the C type system, it's defective but it was easy to implement. Actually fixing C++ needs a ground-up rebuild, that's what you see (impressively) in Sean Baxter's work for example. I doubt it will make a real difference, but to his credit Sean looked at all the horrible problems and rolled up his sleeves and fixed them with a fairly radical set of required language and library changes, rather than kicking them under a rug and insisting it's all fine, just don't look under the rug.
- uecker 2y agoC can essentially do the same thing. I do not see why C++ would have a fundamental advantage here.
- VyseofArcadia 2y agoIt's a social problem. People make mistakes, or are coerced into letting mistakes persist, because you have to get the features out. And if you stand your ground on security, management will find someone who won't. If software "engineers" were engineers who had the duty and authority to say "no, I won't build something unsafe" we'd have fewer problems. Not to say that improved tools isn't also a worthy goal, but why not attack the problem from all angles?
- pyeri 2y agoThose busy humans can always switch to rust or Java? Those ain't bad languages either. And let's face it, given its bare metal nature, there are always going to be limits on how "secure" you can make it in order to "protect" the developer from goofing up in C/C++.
- ngneer 2y agoI agree with almost all of your analysis. One has to be an excellent programmer to write secure code, doubly so in C. I disagree with your conclusion, though. To me, the above means that C FAILS to meet the goal of enabling secure programming. An analogy might be car safety. Yes, if we had perfect drivers we would not even need seatbelts, let alone airbags. But, I would definitely say that a car without these does not meet the goal of "enabling" safe driving merely because it is theoretically possible. https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/812069.pdf https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/...
- hgs3 2y agoMaybe C will finally get slices.
- flohofwoe 2y agoSlices as language features wouldn't be needed if there's just a way to associate a size/count with a pointer. For instance: https://clang.llvm.org/docs/BoundsSafety.html https://clang.llvm.org/docs/BoundsSafety.html https://clang.llvm.org/docs/BoundsSafetyImplPlans.html https://clang.llvm.org/docs/BoundsSafetyImplPlans.html
- raverbashing 2y agoWhile this goes in the right direction, it still feel like wrapping yourself in bubble wrap for safety The true ergonomic solution are slices. Because you don't need boundary checks if you're iterating using the slice count This is definitely a helpful functionality, but it still keeps to C mentality
- flohofwoe 2y agoIf the main goal is hardening existing code bases then this Clang proposal is the right approach IMHO. I think integrating slices "properly" would touch quite a few other areas of the language, which require opinionated design decision by the committee. And committees aren't exactly known for coming up with sensible language design decisions (and looking at C++, I'm fine with the C committee being very conservative). Basically, if I want an actually "better C" for new code, I use Zig. But C is still a valid option for low-level libraries that need to work everywhere (and of course for tapping into the existing library ecosystem).
- raverbashing 2y ago> If the main goal is hardening existing code bases then this Clang proposal is the right approach IMHO. Yup, on this I agree. And I've seen efforts in adding this to existing code
- 2y ago
- lenerdenator 2y ago... that wasn't there before?
- 1oooqooq 2y agoand until it have any widely know undefined behavior it won't be there still.
- deleted 2y ago[deleted]
- pornel 2y agoAlmost every other principle in the charter can be used to shoot down changes for safety, so I'm afraid the safety part is only wishful thinking, hoping for a Sufficiently Smart Compiler/Analyzer to save them. The charter also calls for codifying known solutions, and not new inventions. But the known solutions for memory safety require things like more advanced type systems, that aren't in "the character of the language", and aren't "small and simple". Safety could be improved by severely restricting pointers, in conflict with the "programming freedom" principle, and of course performance overheads or backwards-incompatible changes are unacceptable. So the charter says that C should have a cake and eat it.