stronglink/SUBSTANCE.md at master · btrask/stronglink
c-programmingsecuritysystems-programmingbest-practicesmemory-management
Abstraction: Practical C code quality and security guidelines with rationale
Key points:
- Secure C programs are feasible up to ~25-50KLOC; beyond that complexity makes auditing intractable; keeping code small is the single best security practice
- Memory: always use
callocovermalloc, null pointers immediately afterfree, useexplicit_bzero()for sensitive buffers, avoid custom allocators - Error handling should be ubiquitous; prefer single-line checks (
if(rc < 0) return rc;) to keep them readable and encourage their use - String safety: use
strlcpy/strlcat(OpenBSD),snprintfoversprintf, always pass string literals as format strings to prevent format-string exploits - Build with
-Wall -Wextra -Werror -Wwrite-strings; use sanitizers (Valgrind), fuzzing (AFL), and static analyzers - Reference exemplary codebases: SQLite (gold standard), PostgreSQL, OpenBSD code, Apple CoreFoundation
Connections: Sqlite · Openbsd · C Programming · Secure Coding · Memory Management
Source: https://github.com/btrask/stronglink/blob/master/SUBSTANCE.md