C has a bounded pointer type: A variably-modified type, i.e. a type to a VLA.
int N = 10;
char (*p)[N] = malloc(sizeof *p);
if (!p)
abort();
(*p)[N] = 1; // run-time bounds check possible
f(int n, char buf[static n]);
f(N, *p); // bounds checking possible
-
Example (bounds checking): https://godbolt.org/z/q9qsax7qY
-
Example (static checking): https://godbolt.org/z/xe8oaGad6
-
https://gcc.gnu.org/legacy-ml/gcc-patches/2015-02/msg01565.html
-
https://gcc.gnu.org/legacy-ml/gcc-patches/2015-02/msg01720.html
-
https://gcc.gnu.org/git/?p=gcc.git&a=commit;h=de1b5c17fd1418f01f0a57d03023c21d888dc97c