Secure Coding in C · intermediate · ~10 min
Secure error handling
Fail safely, log enough, leak nothing.
Lesson
Three rules:
- Check every return value of a function that can fail.
malloc, read, open, every syscall.
- Fail closed: on error, deny access / drop the request — never plough through with partial data.
- Don't leak internals: error messages to remote users should be terse. Detailed errors go to your log file.
A surprising amount of CVEs trace to "tested only the success path".