Memory-safe C code
Memory-safe C code
Posted Nov 16, 2025 18:30 UTC (Sun) by alx.manpages (subscriber, #145117)In reply to: Memory-safe C code by khim
Parent article: Rust in Android: move fast and fix things (Google Security Blog)
That was a trivial counterexample. My actual build flags have more than a hundred -Werror= flags, removing not-just-one UB.
What if you could have a -Werror=any-buffer-overflows that would catch any possible buffer overflows? That wouldn't be minor, and we're not far from such a thing in C. The main part of this, I'd like to merge it for GCC 17. Then, there would be some more work needed to make it perfectly safe, but with that part, you could already write much better code.
>> Improving existing C code to be safe is much easier than rewriting the world. Rust still doesn't exist, in a way.
> I would believe it when I'll see it.
All of the improvements I'm researching, I'm applying them to the shadow-utils project. I've already removed many classes of bugs there. The code base is so clean (except in some parts I haven't touched yet), that you can work on it like you'd do in Rust: write some code, iterate until it builds, and once it builds, it should work. Soon, the possibility of buffer overflows in that project will be part of the past. I'd be already surprised if one was found in most parts of it, even if it's theoretically still possible.
> I wish you luck, I'm just not sure if you would be able to do anything, the main problems of C are not technical, they are social
Yup, that's part of the problem. I'm thankful that Rust has done the job of convincing the world that safety is important, and more importantly, feasible. If some projects migrate their C code to more modern dialects, that would be an improvement, even if some code bases are more reticent to that.
> Not sure you can add anything else while staying in C.
A compiler rejecting buffer overflows would also reject much existing code (false positives). However, rewriting the code to appease the compiler would be trivial, and using syntax natural to C programmers. I'm not too worried about that part. You'd change for example pointer syntax to array syntax. Some programmers might reject the idea, but I believe most would accept it, if that brings buffer overflows to an end.
