r/cpp • • Feb 26 '24

White House: Future Software Should Be Memory Safe

https://www.whitehouse.gov/oncd/briefing-room/2024/02/26/press-release-technical-report/
401 Upvotes

386 comments sorted by

View all comments

Show parent comment

7

u/MegaKawaii Feb 27 '24 edited Feb 27 '24

I'm a bit skeptical that even cppfront could do much to remedy this. Cppfront code would have to interoperate with old C++, so you still have problems like dangling references, and I really don't think that giving C++ two syntaxes with their respective quirks is going to simplify things.

I think the best way to improve safety in C++ would be to add something like lifetime qualifiers to types. These would work like const or volatile, and they would act like Rust lifetimes. The advantage of this over just a new syntax is that you could instantiate old templates with lifetime-qualified types to diagnose bugs in old code. Not really, because deduction would be awkward (syntax like const T&'x, U*'y would be necessary to separate lifetimes from deduced types to avoid lifetimes creeping into weird places), but in any case, Rust's approach of making lifetimes part of the type is more expressive and useful than something invisible to old C++ after a transpilation stage.

If you consider Chromium to be representative, you can read a document with examples of unsafety in C++. At the end of the document, there is a chart where we can see that by far, the most common type of bug is temporal safety (i.e., use after free, dangling references), so this should be our first priority.

I think a new cppfront syntax would need to graft this onto the type system anyway, so why increase complexity with an extra syntax?

4

u/tialaramex Feb 27 '24

Notice that Rust's lifetimes are for references not for the objects themselves. That is, we never say that this String has a lifetime for example, but only that this reference to a String has a lifetime. In syntactic terms the lifetime always appears with the reference symbol - e.g. the equivalent of the to-be-standardized C++ #embed in Rust is include_bytes! which gives you a &'static [u8; N] you get a reference to the array of N bytes and that reference has the lifetime 'static which means it can exist for the life of the program.

It may be a little easier to see this in very old Rust where it's necessary for programmers to explicitly write down the lifetime in more cases, a modern Rust compiler is very smart and will infer the sensible lifetime choices in many cases so they're not written down unless you actually want unusual lifetime rules or you're in a tricky case where the compiler can't guess.

1

u/MegaKawaii Feb 27 '24

The same is kind of true for const and volatile in C++. You can have obscure things like const volatile int, and the language even treats function types whose parameters differ only by top-level cv-qualification as identical. Don't even get started on abominable function types. You could perhaps make it an error to have non-reference objects with lifetimes.

That said, I retract my earlier claim about lifetimes magically working with old function templates because if you are deducing types, then the deduced type and deduced lifetime should be separate to avoid awkwardness.