Always ensure buffers, containers, and data structures are properly initialized before passing them to functions that will dereference them. This prevents null pointer dereferences and undefined behavior.
Always ensure buffers, containers, and data structures are properly initialized before passing them to functions that will dereference them. This prevents null pointer dereferences and undefined behavior.
Key practices:
FUNC_ATTR_NONNULL_ALL
and similar attributes to function parameters that must not be nullassert(argc > 0)
rather than assert(argc >= 0)
when zero values are not validos_getenv()
that can return null, check the result before dereferencingExample from the codebase:
// Before: Potential null dereference
if (swap_exists_action != SEA_NONE) {
choice = (sea_choice_T)do_dialog(VIM_WARNING, _("VIM - ATTENTION"), msg.items, ...);
}
// After: Ensure buffer is initialized
kv_resize(*msg, IOSIZE); // Initialize buffer before use
if (swap_exists_action != SEA_NONE) {
choice = (sea_choice_T)do_dialog(VIM_WARNING, _("VIM - ATTENTION"), msg.items, ...);
}
This approach prevents static analysis tools from flagging potential null dereferences and ensures runtime safety by establishing clear contracts about what data must be valid before use.
Enter the URL of a public GitHub repository