C build time NULL deference catcher

This library abuses your C compiler’s dead code elimination optimisations to catch potential NULL dereferences at build time with no runtime overhead.

How does it work?

Consider the following code snippet (NC_PTR is short for NULL-checked pointer):

#define NC_PTR_SAFE_FILE_NAME build_failure_dereference_read_null_c

int foo = 2;
NC_PTR(int*, my_nc_ptr) = &foo;
int my_nc_ptr_value = 0;

srand((unsigned int)time(NULL));
if(rand() & 1) {
NC_PTR_ASSIGN(my_nc_ptr, NULL);
}

/* Errors because we MAY dereference a NULL pointer */
NC_PTR_DEREFERENCE_READ(my_nc_ptr_value, my_nc_ptr);
...

This expands out to:

#define NC_PTR_SAFE_FILE_NAME build_failure_dereference_read_null_c

int foo = 2;
int* _PRIVATE_NC_PTR_DO_NOT_TOUCH_my_nc_ptr =...

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help