Prompt
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:
- Initialize buffers before use: When using dynamic containers like kvec, ensure they are properly sized before passing to functions that access their contents
- Use null safety annotations: Apply
FUNC_ATTR_NONNULL_ALLand similar attributes to function parameters that must not be null - Make precise assertions: Use specific assertions like
assert(argc > 0)rather thanassert(argc >= 0)when zero values are not valid - Handle potentially null returns: When calling functions like
os_getenv()that can return null, check the result before dereferencing
Example 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.