|
|
Log in / Subscribe / Register

Memory-safe C code

Memory-safe C code

Posted Dec 2, 2025 11:17 UTC (Tue) by alx.manpages (subscriber, #145117)
In reply to: Memory-safe C code by anton
Parent article: Rust in Android: move fast and fix things (Google Security Blog)

> Interestingly, in the Linux man-pages 6.03 I see things in the spirit of Deputy, like
>
> void *memmove(void dest[.n], const void src[.n], size_t n);
>
> which indicates how n relates to the passed pointers. Is this a C23 innovation?

C has no such syntax. It was sugar used in the manual pages for documentation purposes.

I used that syntax for a few versions. It's a syntax that was discussed for some time (including in GCC and the C Committee), but we found out it wasn't great. In C, you can't use stuff before declaring it (we had implicit declarations in K&R C, but we got rid of those for good). It can cause cyclic dependencies, etc.

More recently, I've changed the Linux man-pages to use a syntax that is an old GNU extension:

wchar_t *
wmemcpy(size_t n;
wchar_t dest[restrict n], const wchar_t src[restrict n], size_t n);

This is perfectly valid C code for GCC. It looks a bit redundant, but it's okay, because the compiler verifies everything, and it's difficult to make bugs using it.

In the case of memcpy(3), the latest version of the manual pages has

void *
memcpy(size_t n;
void dest[restrict n], const void src[restrict n], size_t n);

which isn't valid because arrays of void are not valid. However, everything else is valid GNU C. I decided to use this sugar with void, for having more useful documentation.

See also: <https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html>.


to post comments


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds