Practical Memory SafetyPractical Memory Safety<br>August 3, 2026<br>rust<br>zig<br>thoughts
Recently:<br>Zig is considering a safe, Fil-C-like compilation target.<br>Fil-C is a “memory safe implementation of C”, and the Zig issue claims this will be “truly memory safe”.<br>This is presented as “unlike Rust” because Rust has escape hatches a.k.a. Unsafe Rust.<br>But it turns out Fil-C’s definition of memory safety is different from Rust’s.<br>So maybe Fil-C is also unsafe?<br>You might look at all this and be like, I Ain’t Reading All That.<br>And you might get the vibe that memory safety is something fuzzy and controversial1,<br>with no right or wrong answers… so maybe you should care less about it?<br>But ’tis not so! Not at all!<br>By the end of this post, I hope to convince you of two seemingly contradictory ideas:<br>Memory safety can be viewed as a spectrum. We can have more of it - which is good even if the details can be nuanced and frustrating.<br>There can be a practical definition of memory safety that isn’t about nuance at all - which is also good, especially for you .<br>So, let’s dig in, starting with the gist of all of this recent hoo-ha ☝️<br>Unsafe Fil-C<br>Adapted from this lobste.rs comment, here’s some C code:<br>struct User {<br>char name[8];<br>int is_root;<br>};
struct User* user = malloc(sizeof(struct User));<br>strcpy(user->name, argv[1]);
This code allocates a User and copies a string from the first command-line argument.<br>But what happens when the input is 8 characters or longer?<br>if (user->is_root) {<br>printf("I am root!\n");<br>} else {<br>printf("I am not root :(\n");
This can print I am root!, even in Fil-C, because strcpy will happily overwrite memory that you own.<br>Fil-C defines memory safety as having having “runtime checks to prevent exploitable memory safety errors”.<br>The above code is not technically “exploitable” so the overwrite is allowed.<br>This code will trap if you write outside the allocation2,<br>so the proposed Zig mode would totally be safer than Yolo-Zig 3, which is a good thing.<br>See also steveklabnik’s comment on HN.<br>Clearly, if Fil-C makes this code more memory safe, then memory safety as a whole must be a spectrum!<br>But would you consider this “an exploit”? Would you consider this code “safe”?<br>Practical Safety<br>These are big questions, and though I love a good technically correct answer,<br>I’d like to contribute a less nuanced answer to the question “what is memory safety” that I think is more practical:<br>Memory-safe code is code where memory has (1) intuitive and (2) easy to follow rules,<br>unless there’s a big sign that says otherwise .<br>In my head, this is also the “non-buoyant water law of memory safety”.<br>Source:<br>RedditSee also:<br>"Is NON-BUOYANT WATER Deadly?", YouTubeThis definition is, obviously, no good for the real experts who design memory models and compilers and CPUs - they’d like to do fun important stuff like proving properties of programs, and eliminating operations that cannot be observed, and so on. \<br>So to be more precise: having a proper technical definition of memory safety is important - insofar as it ensures that safe operations on memory match our general expectations around memory and have rules that can easily be followed correctly.<br>The unsafe side of the boundary can have complex and even obscure rules,<br>but if the safe side also has obscure rules that are hard to follow, is it useful to draw the boundary?<br>Rules that are simple and enforced automatically are easier to follow correctly than rules that are complex and unchecked.4<br>However, if there’s an asterisk on each memory access, then what’s the point of the word “safety” anyway?<br>So, even though Fil-C is safer than Yolo-C and the proposed Memory-Trapping-Zig would be safer than Yolo-Zig,<br>they are not really safe in the only way that matters to you .<br>They still have unmarked and unchecked memory footguns that you need to check and be constantly aware of,<br>and calling that “memory safe” somewhat belittles the title-case “Memory Safety” that people are raving about (and expecting!).<br>Conclusion<br>Memory Safety is great! It works for you : it makes life harder for someone else - the compiler author or the standard library designer - so that you can have a better time working on the things you care about!<br>There can be some subtlety to the definition, and of course there are also issues unrelated to memory safety that are worth systematically attacking, but the point of having memory safety is to have less subtlety overall.<br>Or, to borrow Ralf Jung’s phrasing slightly out of context: “When writing safe Rust, you do not have to worry” about the abstract machine. And really, the world provides enough things to worry about without adding that to the list.<br>Appendix: Some More Examples<br>Python CFFI<br>Python is memory safe: you can use CFFI to do whatever you want:<br>from...