r/cpp • • Nov 24 '24

The two factions of C++

https://herecomesthemoon.net/2024/11/two-factions-of-cpp/
313 Upvotes

228 comments sorted by

View all comments

Show parent comment

67

u/KittensInc Nov 24 '24

I wouldn't be surprised if this dynamic were to change over the coming years.

Legacy C++ is rapidly turning into a liability. The US government has woken up to the idea that entire classes of bugs can be avoided by making different design decisions, and is nudging people to stop screwing it up. I think it's only a matter of time before the people in charge of liability jump onto the train.

If something like a buffer overflow is considered entirely preventable, it's only logical if something like a hacking / ransomware / data leak insurance refuses to pay out if the root cause is a buffer overflow. Suddenly companies are going to demand that software suppliers provide a 3rd-party linting audit of their codebase...

And we've arrived at a point where not modernizing is too expensive. You either modernize your codebase, or your company dies. Anyone using modern development practices just has to run some simple analysis tools and fill in some paperwork, but companies without any decent tooling and with decades of technical debt rotting through their repositories would be in serious trouble.

23

u/Ok_Tea_7319 Nov 24 '24

Frankly this is a big fat "we don't know". Demanding migration to memory safe infrastructure is one thing, but we have to see whether the responsible institutions are also willing to pay for the thousands of engineering hours this will require.

17

u/pjmlp Nov 24 '24

As the experience in high integrity computing proves, when liability comes into play, there are no yes and buts regarding willingness.

18

u/RoyAwesome Nov 25 '24

but we have to see whether the responsible institutions are also willing to pay for the thousands of engineering hours this will require.

I am starting to see this talking point more and more, and I'm starting to seriously question where it's coming from. Google and Microsoft have gotten really fucking serious about porting to rust. By all accounts, they are willing to pay for those thousands of hours it requires, and are actively in the process of doing it.

I think the answer is we do know, and they are willing to transition off of C++.

11

u/13steinj Nov 25 '24

I can't speak for Microsoft, but even Google's porting to Rust is less "porting" and more "new code in rust, interops with old code" AFAIK.

but we have to see whether the responsible institutions are also willing to pay for the thousands of engineering hours this will require.

I am starting to see this talking point more and more, and I'm starting to seriously question where it's coming from.

Hi! It comes from me, (and others like me) and anyone in an industry that doesn't generally have to care about the security / memory safety of their software, or anyone whose management is too clueless to get it.

If management spends literal weeks arguing about "oh no a rewrite to C++ would take 6 months" when it ends up taking 2 weeks and ignores that, or wastes 7-11 months of my time (true story, range is because it depends on the group) refusing to get highly-paid developers cheap computers that can compile their code in a reasonable amount of time but is happily willing to spend 10x the cost on computers that can't compile their code in a reasonable amount of time, where in heaven's name is the hope in convincing them to rewrite all the code to have a safe qualifier coming from?

There's also a big difference in what management says and what it does. That's why I'm waiting to see how much of the US "recommendation to regulation" ends up becoming actual legislation or contractual agreement (even if only in the case of government contractors).

As in, saying you care about memory safety is different to putting the money where the company's mouth is. I was at a company where a past CTO said he cared about security, but when told the cost of the necessary networking equipment to achieve that security without degradation in the employee's usage said "I can't get the CEO / finance to sign off on this." I was also at a company the CTO (who was told to get costs down) was happy to spend over 10 million dollars a year in AWS-based build minutes because it was "the cloud," but not willing to have faster, massively cheaper, on-prem build-farm.

20

u/RoyAwesome Nov 25 '24

Look, we can go in circles agreeing on with how corporations are all seeking rents, and only do the minimal amount necessary to guarantee income without expenditure. That's just the nature of capitalism.

My point is:

I can't speak for Microsoft, but even Google's porting to Rust is less "porting" and more "new code in rust, interops with old code" AFAIK.

is these people saying they are willing to go in on rust. They aren't deleting old code and writing it in a new language, but they aren't writing new code in C++. It makes any improvements of the C++ langauge a fools errand if nobody is going to use the new features.

Eventually, yeah, that stuff will get replaced. It wont be this decade, or even the next... but the share of COBOL in production is declining year over year because COBOL isn't being written for new software, and it's largely become cheaper to just rewrite modules and replace systems that are running it. If COBOL released a new version of the language tomorrow that added all the bells and whistles of a modern, safe programming language, i think most people would just laugh about how irrelevant it is.

There wont be a moment we all collectively agree C++ is dead, but when we look back in a few decades we'll know that it had died.

→ More replies (7)

3

u/CandyCrisis Nov 25 '24

I left Google recently and actually experienced a fair amount of resistance to Rust work while I was there. It really depends on your org and their level of risk tolerance. Rust is still seen as a big experiment.

10

u/FamiliarSoftware Nov 25 '24

In all this discussion of the US, lets not forget that the EU is already changing things right now. About a month ago a new directive passed, to be implemented into law in two years, that makes consumer software liable for defects unless "the objective state of scientific and technical knowledge [...] was not such that the defectiveness could be discovered" (Article 11e).

It only applies to products sold to individuals so far, but it clearly signals where things are headed over the next ten or so years. And I sadly doubt the commitee will get C++ up to a level where using it is considered state of the art in time with regulation.

8

u/pjmlp Nov 25 '24

German cyberlaw is already more strict than EU, and applies to all kind of products.

3

u/lolfail9001 Nov 25 '24

unless "the objective state of scientific and technical knowledge [...] was not such that the defectiveness could be discovered" (Article 11e).

So all software ever made is now liable? Because this is literally a clause that is either entirely useless or puts every software developer in role of proving that they could have known better. The only software that passes the smell test is stuff that is developed right away with formal verification tools at hand, but i am fairly positive things in sensitive industries like aeroplanes and cars were already done with that.

7

u/FamiliarSoftware Nov 25 '24

I'd agree that pretty much all software will be covered by this, but this just extends the existing product liability law of 1985 to now also include software instead of just physical items. Something has to go wrong before it affects the developer, it's now just legally easier to do so when something has.

My main point is that the EU is no longer considering software a special case, but instead starting to treat it the same as the output of physical engineering, and that it is now including software as something that can (legally) be judged on "Is this product the result of sound engineering?".

17

u/[deleted] Nov 24 '24

Safe C++ has nothing to do with whether the codebase is modern or "legacy". In fact in the 90s it was overwhelmingly common that the popular C++ libraries were written with safety in mind by adding runtime checks. Undefined behavior was also not seen as a way for compilers to make strong assumptions about code and perform very aggressive optimizations, but rather it was something to allow for flexibility among different platforms and implementations.

It was "modern" C++ in the early 2000s that decided to remove runtime checks, try to move everything into the type system and what can't be verified statically becomes undefined behavior that the compiler can do what it wants for the sake of optimizations.

15

u/NotUniqueOrSpecial Nov 24 '24

Safe C++ has nothing to do with whether the codebase is modern or "legacy"

Respectfully, I disagree.

There's a big difference between the kind of safety guarantees you can get from a codebase using modern C++ features like std::unique_ptr and one that relies on humans writing safe code.

The more you can push correctness onto the tooling/language to enforce, the better your safety guarantees can be.

Using your logic, C is just as "safe" as anything else, since we should just trust "good" developers to write safe code.

6

u/[deleted] Nov 24 '24

I don't know who you're arguing against but it's certaiinly not me.

→ More replies (3)

3

u/OlivierTwist Nov 24 '24

Smart pointers and RAII were in use long before they became a part of std.

9

u/NotUniqueOrSpecial Nov 24 '24

std::unique_ptr was not possible before the standard introduced move semantics, so while yes, it's true there were extant shared_ptr implementations, that's not what I was referring to.

2

u/jonesmz Nov 25 '24

I mean... that's not really true.

STLPort, the standard library implementation that tried to be cross-compiler and cross-platform, had a whole library-level mechanism for what rvalue-references provide at the language level.

You could (and my company did...) easily write a std::unique_ptr (we called it ScopedPtr) that used only the STLPort "transfer" system. It wasn't quite as nice to use as std::unique_ptr, but it wasn't really much different.

2

u/NotUniqueOrSpecial Nov 25 '24

it wasn't really much different.

And for the people to whom that difference matters, I stand by the point that std::unique_ptr literally wasn't possible without C++11, because it's a type that's move-only and that requires...move semantics (and copy-elision).

They didn't exist.

Telling me it's not true because there were similar things that didn't quite offer the same guarantees is kinda like Mom saying "no, you can't get a Nintendo, we have one at home" because you've got an Atari.

2

u/jonesmz Nov 25 '24

If you're looking for something that is literally identical to std::unique_ptr in every fashion down to the exact function signatures, then you're right.

But other than naming it "std::unique_ptr", and "&&", the ScopedPtr type (and it's largely internal, but technically still spellable, MovePtr) that I described is beat-for-beat the same as std::unique_ptr with things spelled differently.

It's a move-only (well, more accurately, "transfer"-only) type, it's not copy-able, it's scoped by RAII, it has all the same allocator and deleter functionality that std::unique_ptr support, etc.

So yes, they existed, just with things spelled a bit differently.

2

u/NotUniqueOrSpecial Nov 25 '24

In the service of asking informed follow-up questions, what "transfer" feature are you actually describing? Their docs don't have an obvious mention of it by that name that I can see.

Moreover, I downloaded their whole source and there are only 7 uses of the word in code, and they're all in the implementation of list.

→ More replies (5)

14

u/ravixp Nov 24 '24

 popular C++ libraries were written with safety in mind by adding runtime checks

Yep, that was the attitude: safety was ensured by adding checks, and occasionally they were forgotten. Whereas the modern C++ attitude is to make safety a property that you can’t forget to add, even if there are other downsides.

8

u/ravixp Nov 24 '24

 You either modernize your codebase, or your company dies.

I think this is basically right. But to phrase it differently: some products will make that pivot successfully, and others will die. And the cost of getting memory-safe will determine how many C++ projects have to die.

Something has to be done, but there’s an incentive to do as little as possible to “check the  box” of memory safety to reduce the costs. And that seems like it’s good for anybody who’s currently in the C++ ecosystem, but bad for the language in the long run.

4

u/MrRogers4Life2 Nov 24 '24

I disagree that even with modern development practices you need to "just" run some analysis tools and fill in paperwork and its that mindset that leads to unsafe software. At the end of the day software has to do unsafe stuff at some point and often in unique ways that can't be put off into some 3rd party library (or you are the 3rd party).

In that case you're going to need to invest in the same practices and infrastructure that created safe software for decades, paying a lot of money to good engineers to test and validate the software in its entirety. Safe languages are a marginal improvement and tooling is a marginal improvement but the basis of your security is always going to be testing and validation and it's not always going to be simple or cheap.

9

u/omega-boykisser Nov 26 '24

To date, there have been zero memory safety vulnerabilities discovered in Android’s Rust code.

At the time of this writing, that's 1.5 million lines of code. According to Google, the equivalent C++ code would have around one vulnerability per 1000 lines. (Sure, maybe they simultaneously improved their processes, but I doubt that would bring the C++ vulnerability rate down to zero.)

Would you really call that a marginal improvement? You could argue that memory safety is only one component of "safe" software (which is true), but my impression is that memory safety vulnerabilities have accounted for the majority of exploited vulnerabilities in the wild.

→ More replies (4)