|
|
Log in / Subscribe / Register

Memory-safe C code

Memory-safe C code

Posted Dec 13, 2025 23:48 UTC (Sat) by sammythesnake (guest, #17693)
In reply to: Memory-safe C code by alx.manpages
Parent article: Rust in Android: move fast and fix things (Google Security Blog)

I'm not a C programmer* but I have aa feeling that you still have problems with the following:

int a[] = []; /* or however you spell this in C */

j(0, a)

because

a[countof{a} - 1]
is out of bounds, right?

* By firm choice - every time I even think about C, I'm bemused that anyone would choose a language apparently composed entirely of foot guns. If I ever need anything closer to the iron then higher level languages, I'll go for rust, but I've never had enough reason to actually learn it properly so far. YMMV, of course!


to post comments

Memory-safe C code

Posted Dec 14, 2025 0:16 UTC (Sun) by alx.manpages (subscriber, #145117) [Link]

Yup, I got a mistake in the example. It should have been:
void j(size_t n, int a[n])
{
    a[countof(a) - 1] = 42;  // diagnostic
    if (countof(a) >= 1)
        a[countof(a) - 1] = 42;  // ok
    a[countof(a)] = 7;  // diagnostic

    for (int i = 0; i < countof(a); i++)  // ok
        a[i] = 0;
    for (int i = 0; i < countof(a) + 1; i++)  // diagnostic
        a[i] = 0;

    a[42] = 0;  // diagnostic (42 not proved within [0,countof(a)).
}


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