by sylware · 4y ago · 0 comments
For instance in the whole network stack, some "agents of the planned obsolescence" did not use constant expressions in switch/case statements or initializers. Also tell those who are using that toxic c11 "Generic" keyword to use explicit code instead. I think, the main issue for C now is the standard body trying to obsolete stable in time C code and forcing upon devs/integrators the usage of the only few compilers which are able to follow fast enough their tantrums (latest gcc/latest clang). Results: all attempts to provide "working" alternative C compilers is beyond reasonable. This is becoming so accute, it is more likely to be a worldwide scam than anything else.
Yep, open source software is not enough anymore, it needs to be lean, simple but able to do reasonably "the job", and ofc very stable in time hence C89 only with benign bits of c99/c11, or "standard assembly".
As you seem to care about this, have you made this argument on LKML yourself? What Linux kernel related work are you active on that drives your position?
Its nicely documented on LWN https://lwn.net/SubscriberLink/885941/01fdc39df2ecc25f/
It was also discussed here on HN https://news.ycombinator.com/item?id=30459634
I mean you can't even write an allocator in standard C.
I might be missing something, but I don't believe this is true? The type aliasing rules have an explicit carve-out for `char *` aliases of other types, which is intended to solve this exact issue.
Similarly, C99 and C11 both allow aliasing through a union of pointers without violating the strict aliasing rule.
malloc itself is specified to return a void pointer, but the effective type established during assignment circumvents what would otherwise be UB via aliasing[2].
Edit: Specifically, the reasoning is:
1. Allocated objects have no declared type;
2. 6.5.6: Objects with no declared type have the type of their accessing lvalue
3. 6.5.7: Access of a stored value is valid for lvalue expressions that are type compatible with the effective type.
So there's no UB here. `malloc` itself doesn't need to know about the effective type produced by the lvalue, and C (at least C99 onwards) respects that type for strict aliasing purposes.
#include <stdlib.h> /* for size_t */
static char data_storage[10000000];
static char *nextp = data_storage;
void *malloc(size_t size)
{
void *p = nextp;
nextp += size;
return p;
}
void free(void *p){}This is because of the particulars of the C language. For instance, the way it handles arrays and pointer arithmetic make it difficult to detect every instance of out-of-bounds access.
[0] -fwrapv, see https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
Using out of bounds references in arrays and pointer arithmetic are unsafe but they can be defined. The definition of "array accesses using indexes that are outside the range of 0 - the declared array size will access memory in the data segment. The compiler will treat that access as if the addressed memory had the type specified for the array." The behavior is defined, it is unsafe, but it is defined. Not like "the compiler may or may not choose to optimize out loads and stores, or re-order them." which especially on embedded systems creates bugs where the things like "read the status register THEN read the data register (which clears the status register when read)" those get re-ordered and suddenly your loop never exits because your status bit is never set. That kind of UB needs to die in a fire IMHO.