3 ms·
Thinking about this at a higher level, the real problem is that we use C arrays at all. But we can't stop using C arrays, because libc and a huge body of other
by kelnos 4y ago
Thinking about this at a higher level, the real problem is that we use C arrays at all. But we can't stop using C arrays, because libc and a huge body of other C libraries out there represent lists of data using C arrays.
As a useful counterexample, glib has GArray, GPtrArray, and GList, along with functions that you use to manipulate the lists. Those functions are presumably implemented correctly, and will prevent you from making out-of-bounds accesses.
But of course those things are not widely adopted, because they're not standardized, they're not in libc, and libc has not been reimagined such that all of its APIs take and return these safer list primitives instead of bare C arrays.
(And sure, GArray etc. are not necessarily suitable everywhere, as they require heap allocations. But I could imagine a safe abstraction that could allow for using stack or static allocations to back it.)
So I see something like Checked C as a band-aid, when the language and stdlib just needs a complete overhaul. Obviously that's incredibly unlikely to ever happen, so something like Checked C is a decent compromise, I guess.
- sramsay 4y ago> As a useful counterexample, glib has GArray, GPtrArray, and GList, along with functions that you use to manipulate the lists. Those functions are presumably implemented correctly, and will prevent you from making out-of-bounds accesses. > But of course those things are not widely adopted, because they're not standardized, they're not in libc, and libc has not been reimagined such that all of its APIs take and return these safer list primitives instead of bare C arrays. I'm really not sure why the second thing is regarded by C programmers as such an insuperable barrier to the first thing. I absolutely understand that there are circumstances where extreme portability is a major requirement. That, and C's ability to operate in highly constrained environments are among its most laudable features. But many of us (including me) write user-facing C applications that have neither requirement and where Glib is either already installed or easy to install. Why not just go ahead and use it? If you're trying to get it to run on a tiny embedded system or in some land without Glib . . . well, this isn't the program for you! I feel like this is more of a weird cultural prejudice than a technical matter. People run programs in JavaScript (or Haskell, or Python) with hundreds of dependencies, and while some find that annoying, I don't hear people in these communities objecting to idea of dependencies the way people do with C. Could the standard library be better? Of course. But there's something to be said for not including a lot of batteries in the standard and just using the third-party tools that are available.
- int_19h 4y agoBut then why even use C?
- kccqzy 4y agoGPtrArray doesn't tell you the type of the pointer inside the array. You are throwing away type safety. This is a regression rather than an improvement. It's like making every array void*a[N]. When I used to write GLib code, I can't count how many bugs were introduced when I thought the GPtrArray contained a type of pointer but it contained some other type. Also, which part of GPtrArray prevents you from making out of bounds access? Last time I checked it doesn't.
- xigoi 4y ago> GPtrArray doesn't tell you the type of the pointer inside the array. Well, that's also C's fault for not having parametric polymorphism.