You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nullsafe C adds NULL checks to catch errors at compile-time, not runtime. It is 100% compatible with existing C codebases and can be used incrementally to identify safety issues at compile-time.
5
+
Nullsafe C adds NULL checks to catch errors at compile-time. It is 100% compatible with existing C codebases and can be used incrementally to identify safety issues at compile-time.
6
6
7
-
You can annotate your code with `_Nonnull` to presere narrowing.
7
+
This provides the following benefits:
8
+
* This makes the code safer by reducing the number of potential runtime null dereferences
9
+
* Improves developer experience by shifting errors left
10
+
* Makes the code more readable
11
+
* Adds type errors that other more modern languages have (Rust, TypeScript, Kotlin)
8
12
9
13
**Try it online:**[Interactive Playground](https://cs01.github.io/llvm-project/) - See null-safety warnings in real-time in your browser!
10
14
11
-
It does this by making two changes:
12
-
1. All pointers are nullable by default, unless explicitly marked `_Nonnull`. Clang already allows the code to be annotated with `_Nullable` and `_Nonnull`, but this compiler treats all unmarked pointers as nullable by default.
13
-
2. The compiler tracks when you've null-checked a pointer and knows it's safe to use. When you write `if (p)`, the type system understands `p` is non-null in that branch.
15
+
Nullsafe C treats all pointers as potentially null ('nullable') unless it is certain they are not. It does this in two ways.
14
16
15
-
## Example
17
+
The first is by semantic analysis: if you test a pointer with `if(p)`, then it knows that branch contains a non-null pointer.
18
+
19
+
The second is by using Clang's [`Nullability`](https://clang.llvm.org/docs/AttributeReference.html#nullability-attributes) attributes, in particular `_Nonnull`. If a pointer is marked as `_Nonnull` the compiler will require a pointer it knows it not null is passed to it. This can be done either by passing a `_Nonnull`-annotated pointer, or by doing type narrowing.
20
+
21
+
If using a compiler other than clang, you can add `#define _Nonnull` as a no-op. You will not get the same compile checks as with Nullsafe C (clang fork), but the compillation will still succeed without error.
22
+
23
+
## Examples
16
24
17
25
```c
18
26
voidunsafe(int *data) {
19
-
*data = 42; // warning - data might be null!
27
+
*data = 42; // warning: dereferencing nullable pointer of type 'int * _Nullable'
20
28
}
29
+
```
30
+
[Try it in the interactive playground](https://cs01.github.io/llvm-project/?code=dm9pZCB1bnNhZmUoaW50ICpkYXRhKSB7CiAgKmRhdGEgPSA0MjsgLy8gd2FybmluZzogZGVyZWZlcmVuY2luZyBudWxsYWJsZSBwb2ludGVyIG9mIHR5cGUgJ2ludCAqIF9OdWxsYWJsZScKfQ%3D%3D)
21
31
32
+
Type narrowing:
33
+
```c
22
34
void safe(int *data) {
23
35
if (data) {
24
36
*data = 42; // OK - data is non-null here
25
37
}
26
38
}
39
+
```
40
+
[Try it in the interactive playground](https://cs01.github.io/llvm-project/?code=dm9pZCBzYWZlKGludCAqZGF0YSkgewogIGlmIChkYXRhKSB7CiAgICAqZGF0YSA9IDQyOyAvLyBPSyAtIGRhdGEgaXMgbm9uLW51bGwgaGVyZQogIH0KfQ%3D%3D)
27
41
42
+
Anontated with `_Nonnull`:
43
+
```c
28
44
voidsafe_typed(int *_Nonnull data) {
29
-
*data = 42; // OK - data is known to be non-null by the compiler
45
+
*data = 42; // OK - we know data is not null so we can derefernce it
30
46
}
31
-
32
47
```
33
-
Try it out in the [Interactive Playground](https://cs01.github.io/llvm-project/).
34
-
35
-
## Installation
48
+
[Try it in the interactive playground](https://cs01.github.io/llvm-project/?code=dm9pZCBzYWZlX3R5cGVkKGludCAqX05vbm51bGwgZGF0YSkgewogICpkYXRhID0gNDI7IC8vIE9LIC0gd2Uga25vdyBkYXRhIGlzIG5vdCBudWxsIHNvIHdlIGNhbiBkZXJlZmVybmNlIGl0Cn0%3D)
Or download manually from [releases](https://github.com/cs01/llvm-project/releases).
52
60
61
+
On mac you may need to do the following:
62
+
```bash
63
+
brew install zstd
64
+
xcode-select --install # If not already installed
65
+
```
66
+
53
67
### Windows
54
68
55
69
Builds not available at this time, you must clone and build locally.
@@ -62,7 +76,7 @@ Each release includes:
62
76
63
77
### IDE Integration
64
78
65
-
Once installed, configure your editor to use the null-safe `clangd`:
79
+
Once installed, configure your editor to use the null-safe `clangd`. Install the `clangd` extension from llvm and set the path to the clangd binary you just downloaded.
0 commit comments