To get to memory safety with C:
- Add support for array bounds checking. Ideally with the compiler doing the heavy lifting and providing to itself that most runtime bounds checks are unnecessary.
- Implement trivial dependent types so the compiler can know about the array size field that is passed next to a pointer. AKA
void do_something(size_t size, entry_t ptr[size]);
- Enforce the restrict keyword. This is actually the tricky bit. I have some ideas for a language that is not C, but making it backwards compatible is beyond where I have gotten. My hint is separation logic.
- Allow types to change safely. So that free() can change the type of the pointer passed to it, to be a non-dereferencable pointer (whatever bits it has).
This is an idea from separation logic.
Allowing functions to change types of data safely could also be a safe solution to code that needs type punning today.
I think conceptually modules are great, but if your goal is source compatible changes that bring memory safety then something like modules is an unnecessary distraction.
Any changes that ultimately cannot be implemented in the default C compiler I don't think will be preferable to just rewriting the code in a more established language like Rust.
On the other hand I think we are in a local maxima with programming languages and type systems. With everyone busy recombining proven techniques in different ways instead of working on the hard problem of how to have assignment, threading, and memory safety. Plus how to do proofs of interesting program properties with things like asserts.
Unfortunately it appears that only through proof can programs be consistent enough that specific security concerns can be said to not be problems.
What I have seen of ADA Spark lately has been very tantalizing.
I have a personal project that I think I have solved the memory safety problem, while still allowing manual memory management and assignment. Unfortunately I am at a stage where everything is mostly clear in my head, but I haven't finished fleshing it out and proving the type system, so I really can't share it yet :-(.
While implementing modules, memory safety, type variables, and functions that can change the types of their argument pointers. I think I will end up with something simpler than C in most respects.
I keep going well that doesn't make any sense today, as I go through all of the details and ask why is something done the way it is done.
One of those questions is why doesn't C use modules.