9 ms·
And that's new code, not legacy. Embarrassing. I would have expected better from something written in this century. C++ does sort of have a safe subset. If you'
by Animats 7y ago
And that's new code, not legacy. Embarrassing. I would have expected better from something written in this century. C++ does sort of have a safe subset. If you're passing raw pointers all over the place today, you're doing it wrong.
- fabrice_d 7y ago"sort of" is the key. If the language doesn't prevent you from shooting yourself in the foot, on any large project bad things will happen. Safer languages are better tools because they make obvious when you're stepping out of the safe zone, which is not the case with C++, even modern releases.
- _se 7y agoPassing raw (non-owning) pointers is fine, even if references would be better. Heap allocating with new instead of a smart pointer is the problem.
- zetafunction 7y agoNon-owning pointers are absolutely a problem if the object graph is large and complex enough. Many objects in Chromium have lifetimes managed by smart pointers, but unfortunately, that doesn't do anything to protect against code that mistakenly violates an invariant like "object X must always outlive Y due to these non-owning pointers". The problem is C++ turns a violation of lifetime invariants into undefined behavior rather than safely crashing.
- scarmig 7y agoAt some point, the idea that a tool is fine, it's just that everyone is using it wrong starts to become a reason to look for alternatives to the tool. Chromium is a large cpp project, with lots of resources behind it, but people still fail to write safe code for it. There are small bore efforts that can be done to tangibly improve things, but in the end it seems like the real solution is to start writing components in a language that offers better memory safety and maintains high performance. That is a huge, risky effort as well, of course.
- jldugger 7y agoMaybe they're just doing better on all fronts?
- reubenmorais 7y ago> C++ does sort of have a safe subset. I see this claim all the time, but can you give some examples of large C++ projects that don't constantly struggle with memory safety issues? (And are looking for them, of course.)
- pjmlp 7y agoThe problem is enforcing that safe subset. It only works when everyone plays balls and doesn't do C style coding, ever. Usually that can work in small teams with security minded individuals but it isn't a given. And then there is the little fact that on most surveys, the amount of answers referring any kind of static analysis tooling are usually around 50%. What C++ has definitely going for it, is having the type system tools to write much safer code than plain old C. However its copy-paste compatibility with C is also what hinders any attempt to force people to actually only use those better features. The only way to fix this is having systems programming languages being adopted that aren't C at copy-paste level. Other would be some kind of Safe C, but both WG14 and C community in general have voted against such improvements.
- josteink 7y ago> It only works when everyone plays balls and doesn't do C style coding, ever. If that’s the problem, and solving that would solve all of C++s memory-issues, why have no-one made a compiler option to simply make that code illegal? A -EUNSAFE or whatever?
- pjmlp 7y agoBecause what got C++ adopted in first place was its copy-paste compatibility with C. C++ was also born at Bell Labs, and due to that, all major C compiler vendors quickly started shipping C++ on their boxes as well. If you take away copy-paste compatibility you might be better off doing D, C# or whatever safe variant already exists. Which is what many of us have done, to move to type safe languages, and only use C and C++ at the boundaries, in small pockets of unsafe code. In fact if you look at mobile OSes, that is the reality for app developers, C and C++ are no longer the full stack languages they were 20 years ago, rather used for the kernel, drivers, compositor and shading languages, but everything else happens in safer languages. And the SDKs only allow you to write libraries, not full applications. Naturally are clever developers that subvert the workflow and transform the libraries into the actual application.
- saagarjha 7y agoAnecdote: I had to track down a bug in one of my programs once because a vexing parse caused the compiler to emit a call to the wrong specialization of a constructor, leading to certain fields remaining uninitialized. This was in a small, greenfield project that I wrote using the full force of modern C++17, using the safest, most idiomatic constructs I could find. The fact that no compiler or sanitizers made so much as a peep makes me feel that memory unsafety is embedded so deep into the core of the language that extracting out “the good parts” is probably of similar complexity to just detecting all the problems ahead of time. (That is: impossible.)
- logicchains 7y agoThis can be avoided to some degree by the practice of giving all primitive members a dummy in-class initialiser. E.g. int64_t mSomeIndex=-1; Sadly I don't think the tooling exists to enforce something like that automatically, because C++ is so hard to parse (a separate issue to memory safety). But when/if metaclasses finally land, it should be possible to write a "fixed" class/struct keyword that default initialises all fields.
- saagarjha 7y agoThis was an enum (class) member, so I didn’t really have one to spare that wouldn’t just hide the issue under the rug.
- logicchains 7y agoAh, that sucks. I don't think there's a solution to that apart from just adding an "Invalid" field to every enum class and initialising to that, but checking for that (asserting not invalid) adds ugly boilerplate to every usage of every enum.
- saagarjha 7y agoRight. Type systems generally don’t like having invalid states at any point, because after all that’s kind of what they do :)
- TwoBit 7y agoC++ coding conventions for most organizations should ban use of manually managed raw pointers.
- Tempest1981 7y agoThere's also the guideline support library (GSL) -- is anyone using it? https://github.com/Microsoft/GSL https://github.com/Microsoft/GSL Stroustrup talk from 2016: https://www.youtube.com/watch?v=JtMPGwA3MzQ https://www.youtube.com/watch?v=JtMPGwA3MzQ
- lmm 7y agoEvery C++ fan claims that this safe subset exists, but none of them can tell you what it is, or how to tell whether any given library is written in it or not.
- pnako 7y agoSo-called "modern C++", using std::string, RAII, unique_ptr (and shared_ptr). It's not perfect but it's certainly a giant leap from old-school C. It's something I don't see Rust proponents address. It's easy to build a straw man argument of Rust vs. old-school C, but it's a more natural path to go from C to modern C++ than to go from C to Rust. You get to keep your compiler, build system, tools, libraries and indeed existing code.
- josteink 7y agoRust supports incremental rewrites of existing C-codebases too. See this for an example: https://news.ycombinator.com/item?id=21681395 https://news.ycombinator.com/item?id=21681395
- the_why_of_y 7y agoSee also the example of librsvg, a C implementation of SVG that was incrementally translated to Rust by some volunteers over a couple years. https://people.gnome.org/~federico/blog/exposing-c-and-rust-apis.html https://people.gnome.org/~federico/blog/exposing-c-and-rust-...
- kelnos 7y ago> So-called "modern C++", using std::string, RAII, unique_ptr (and shared_ptr). And yet we see the same memory-safety bugs come up time and time again in supposedly-modern C++ programs. So either this modern way is not enough to avoid these classes of bugs, or people very quickly fall to the temptation to use unsafe constructs due to performance or just because it's easier to write. We're humans. If there's an easier way to do something, even if it's less safe, we'll invariably do it sometimes. I like that Rust makes it harder to do so, and makes you explicitly say that you want to do something unsafe, which I imagine deters a lot of people from going down those paths. And when someone writes a memory-safety bug in unsafe Rust, they get much more egg on their face than if they were to write the same bug in C++.
- zetafunction 7y agoChromium's object graph, for better or worse, has a lot of nodes and edges. Operations like tearing down a document that's navigating away are full of complexity. Executing JS is fraught with peril, since it's possible that objects currently on the C++ stack will get destroyed by an operation triggered by user JS. The modern web is just really complicated.
- littlestymaar 7y ago> C++ does sort of have a safe subset. The “sort of” is what matters though: - With std::unique_ptr, you trade use-after-free for use-after-move. Rarer, but still a threat. - std::shared_ptr is costly which makes it unsuitable for some uses, and you can still have data rave with it if you don't use it correctly.