How (memory) safe is Zig? (2021)

scattered-thoughts.net

36 points by vortex_ape 13 hours ago


90s_dev - 12 hours ago

> But it does not nearly approach the level of systematic prevention of memory unsafety that rust achieves.

Unless I gravely misunderstood Zig when I learned it, the Zig approach to memory safety is to just write a ton of tests fully exercising your functions and let the test allocators find and log all your bugs for you. Not my favorite approach, but your article doesn't seem to take into account this entirely different mechanism.

pizlonator - 11 hours ago

> it seems impossible to secure c or c++

False. Fil-C secures C and C++. It’s more comprehensively safe than Rust (Fil-C has no escape hatches). And it’s compatible enough with C/C++ that you can think of it as an alternate clang target.

dang - 10 hours ago

Related:

How safe is Zig? - https://news.ycombinator.com/item?id=31850347 - June 2022 (254 comments)

How Safe Is Zig? - https://news.ycombinator.com/item?id=26537693 - March 2021 (274 comments)

How Safe Is Zig? - https://news.ycombinator.com/item?id=26527848 - March 2021 (1 comment)

How Safe Is Zig? - https://news.ycombinator.com/item?id=26521539 - March 2021 (1 comment)

nanolith - 12 hours ago

There is a third category of memory and other software safety mechanisms: model checking. While it does involve compiling software to a different target -- typically an SMT solver -- it is not a compile-time mechanism like in Rust.

Kani is a model checker for Rust, and CBMC is a model checker for C. I'm not aware of one (yet!) for Zig, but it would not be difficult to build a port. Both Kani and CBMC compile down to goto-c, which is then converted to formulas in an SMT solver.

Dwedit - 11 hours ago

If you're filling uninitialized pointers with AAAAAAAA, it might be best to also reserve that memory page and mark it as no-access.

I'm not even joking. Any pattern used by magic numbers that fill pointers (such as HeapFree filling memory with FEEEEEEE on Windows) should have a corresponding no-access page just to ensure that the program will instantly fail, and not have a valid memory allocation mapped in there. For 32-bit programs, everything past 0x8000000 used to be reserved as kernel memory, and have an access violation when you access it, so the magic numbers were all above 0x80000000. But with large address aware programs, you don't get that anymore, only manually reserving the 4K memory pages containing the magic numbers will give you the same effect.

BrouteMinou - 6 hours ago

I don't know why we are still having this topic going on. Zig is not safe, period.

Zig gives you the control you need if that is what you want, safety isn't something Zig is chasing.

Safer than C, yeah, but not safe.

Rust = safe Zig = control

Pick your weapon for the foe in front of you.