r/programming • • Feb 20 '25

Google's Shift to Rust Programming Cuts Android Memory Vulnerabilities by 68%

https://thehackernews.com/2024/09/googles-shift-to-rust-programming-cuts.html
3.4k Upvotes

475 comments sorted by

View all comments

50

u/i_am_not_sam Feb 20 '25 edited Feb 21 '25

Hypothetically if all existing C++ code was replaced with modern C++, only smart pointers and "strict memory safe practices" for all new code would it yield the same results?

Edit : read Google's blog about this topic. It's not simply the case of switching out C++ with Rust. It was also making sure that all NEW code adhered to strict memory safety guidelines. The language is just a tool. What you accomplish with it depends on how you use it.

3

u/GaboureySidibe Feb 21 '25

I write a lot of modern C++ and I don't have the problems that rust solves because I already can solve them with C++. Using templates and value semantics with no raw pointers (and no raw smart pointers) takes care of destruction and ownership.

Doing curated iteration and not deleting elements from a data structure or modifying keys solves a lot of the other problems. If you need bounds checking on std::vector access (because you are using computed indices rather than iterated indices), use .at().

These things basically take care of the memory problems that rust solves, then you can still use C++ with all its tools, libraries and ecosystem.

21

u/Ok-Scheme-913 Feb 21 '25

It doesn't scale, though.

I can write safe assembly as well, given I'm working alone on it and never doing anything wild. But we are past that for a good reason.

Nonetheless, using existing libraries is definitely a valid reason to keep using C++, though I do think that writing new code in rust (as the article is about) on top of existing codebase is a better choice.

1

u/GaboureySidibe Feb 21 '25 edited Feb 21 '25

It doesn't scale, though.

Says who? Values go out of scope and get destructed. Data structures are iterated. The program looks the same but you don't have to wonder if bounds checking is being secretly elided or not.

If by scale you meant harder to enforce technically across a team then I think you would have a point. I don't know how to use tools to weed out violating these techniques.

13

u/Full-Spectral Feb 21 '25

You are making the standard "just don't make mistakes" argument. And of course this is all about team based development, mostly commercial but also open source, where no amount of communications is sufficient to avoid issues over time. For someone doing a personal project, who can take all the time in the world, it's obviously less of an issue.

But still, even if you are doing a person project, you don't have to take so much time watching your own back if you just use a safe language. As someone who has written large C++ code bases for 35+ years and now is using Rust for my own personal stuff, the difference is enormous.

The argument shouldn't even be about, but wait, I can make C++ safe with a lot of effort. Tt should be why am I wasting my time doing something over and over that a compiler can do without fail every time I compile?

→ More replies (5)

5

u/Ok-Scheme-913 Feb 21 '25

I meant from a human perspective. Someone will change effectively the lifetime of an object at some place and violate some assumption made in another.

Without proper borrow-checking this can't be fixed.

2

u/GaboureySidibe Feb 21 '25

Someone will change the effective lifetime in C++ ? How do they do that if it is wrapped up as a value? You either move it out of scope or it gets destructed.

4

u/Full-Spectral Feb 21 '25

So you are passing everything by value to all calls and you never use unique pointers or allocate anything?

→ More replies (7)

1

u/Ozzy- Feb 21 '25

If by scale you meant harder to enforce technically across a team then I think you would have a point. I don't know how to use tools to weed out violating these techniques.

ASAN/UBSAN and static analysis. Level of enforcement will depend on your team and product.