Memory-safe C code
Memory-safe C code
Posted Nov 16, 2025 21:22 UTC (Sun) by khim (subscriber, #9252)In reply to: Memory-safe C code by alx.manpages
Parent article: Rust in Android: move fast and fix things (Google Security Blog)
> I don't understand what you mean by 'validness'. I assume you mean provenance?
I mean: how would your compiler know whether to accept the program or reject it. Rust uses lifetimes for that and that use, in turn, means that it needs to mark them on all the functions that return references and then on structures that may carry references and then on pointers to functions (because now function that receives pointer to first element and function that returns pointer to the second element are different) then you need borrow checker and so on.
It's huuuuge can of worms to do that properly.
> If so, I agree that GCC should add an attribute such as[[gnu::returns_derived_ptr()]], which would add that information to the API. There exist similar attributes already, so this wouldn't be very difficult.
Bwahaha. Try it and you'll find hundreds of use-cases that seem “natural” yet are rejected by a naïve validator.
Or, better yet, you may read papers that describe these things. You'll need borrows, reborrows, split borrows and bazillion other things to make it ergonomic. And then, after all that… you would still need to rewrite all code to ensure that complies with these rules.
You would probably need to spend 5 to 10 years to just plug all the loopholes in your typesystem… and “let's rewrite it in Rust” guys wouldn't wait all that time thumbing their fingers.
> That was solved already in C23. strstr(3) now returns QChar*, which is as qualified as the input.Typical story of disruptive technology: it's really nice to know that C have done first two steps (out of hundred) needed to achieve memory safety… but would anyone still care 10 (or 20 or 30…) years down the road when it would finally traverse it and provide something that actually works? We would see, but past history is not encouraging.
It's like Bucyrus: it understood that hydraulics is the future very early, tried to build something that his customers would buy for half century… but their customers had no need for underpowered hydroshovel and the ones who needed it knew hydroshovels are made by Caterpillar, not by Bucyrus… thus the whole thing ended with Caterpillar buying them, after half-century. Similarly here: even if after 5-10-20-30 years all that stuff would be finished… you'll find no users for it: people who are willing and capable of rewriting things would be using Rust by then, and the remaining guys would continue to use C-as-it-exist-today, because they just don't want or couldn't rewrite everything.
> If you show them a C code base that is as safe as Rust in terms of buffer overflows, and adding some attributes in some function prototypes is the only thing that needs to be doneNope, that's precisely the issue: that's just a tiny part of “what needs to be done”. Even such a simple structure as a doubly-linked list is more-or-less impossible to describe in these terms. Rust solution is to give you an unsafe-based LinkedList that you are supposed to use in place of your ad-hoc lists.
For many C codebases this would imply more-or-less total rewrite.
> There's a difference between adding attributes to function prototypes and entirely rewriting the code.Yes. And for small, really small percentage of C code the former approach may work. For the majority the latter is strict requirement. As in: more-or-less full rewrite. Because too many data structures couldn't be handled with a markup. And to ensure that you code is safe you need small amount of well-designed and tested implementations used by everything… which is precisely the worst side of C/C++ in practice: practically everyone creates their own ad-hoc data structures instead of reusing them.
Worse yet: benefits for the codebases that belong to the latter category (just add markup and everything works) is negligible (they, usually, don't have any memory bugs to begin with because if lifetime story is so simple that you can trivially describe to the compiler then you can describe it to the fellow developer, too), while for codebases where you really want to make it approach work (because they have very complicate story about liveness of the variables in their codebase) you need a rewrite.
So your improvements to C would benefit projects that barely need them yet would leave unfixed bugs in complicated codebases that suffer from memory unsafety worst.
The LWN site is currently under high scraper load, so comment display has been suppressed for anonymous users. If you are a human, you may read the comments by clicking the button below:
Note: you can avoid this step in the future by logging into your LWN account.
