Safe C

Safe C

C with memory safety guarantees as strong as Rust.

Safe C is a fork of Clang with one flag, -fsafe-c. Code it cannot prove safe does not compile, or stops at run time instead of corrupting memory. Its type annotations (_Owned, _Nullable, __counted_by) are macros from one header, so the same file is still C for any other compiler.

Try it: in the browser, or brew install safe-c-project/tap/safe-c and then safecc file.c.

Or use it only as a checker: run safecc -fsyntax-only file.c in CI and keep building with gcc, MSVC or clang. That catches the ownership and borrow errors; the run-time bounds checks need a safecc build.

Compared to Rust

Rust Safe C
&T (never null) T *
Option<&T> T *_Nullable, checked before use
&[T] T *__counted_by(n)
Box<T> T *_Owned
Option<Box<T>>, Option::take T *_Owned _Nullable, _Take(place)
&mut T / &T T * / const T *, for what may be freed or replaced (see below)
&'static T T *_Static
struct S<'a> struct _View S: locals and parameters only
*const T T *_Unchecked, followed only in a _Trusted function
a safe function with an unsafe body _Trusted
unsafe { } __unsafe { } (does not lift ownership: that is _Trusted)
enum with data, match union _Tagged_by(kind), plain data only
Mutex<T> _Guarded_by(lock)

What is different

More

Apache License v2.0 with LLVM Exceptions, like LLVM.

View the source of this page on GitHub