GuideOpen
C - Language-Specific Review Notes
Load this file alongside rules/universal.md. Universal rules are not repeated here — only C-specific rules and idioms.
Front matter
language: c
extensions: [".c", ".h"]C — Language-Specific Review Notes
Load this file alongside rules/universal.md. Universal rules are not repeated here — only C-specific rules and idioms.
PR Analyzer — C Risk Signals
printf/ debugfprintf(stderr, ...)statements left in production code// TODO/// FIXMEcomments near memory management code — high risk- Disabled compiler warnings (
#pragma GCC diagnostic ignore,-wflags in Makefile) - Hardcoded credentials or keys in source
- Use of banned functions:
gets,strcpy,strcat,sprintf,scanfwithout width limits
Code Quality — C Checks
- Functions longer than 50 lines — C functions tend to grow organically and become hard to reason about
- Missing
NULLcheck aftermalloc/calloc/realloc - Return value of functions ignored without explicit
(void)cast - Global mutable state used across translation units without clear ownership
- Magic numbers without
#defineorconst— especially sizes and offsets - Mixed
malloc/freeownership — unclear which caller is responsible for freeing
Security
- Flag
gets()— no bounds checking, always a buffer overflow; replace withfgets() - Flag
strcpy()/strcat()— usestrncpy()/strncat()with explicit size, orstrlcpy()/strlcat() - Flag
sprintf()— usesnprintf()with explicit buffer size - Flag
scanf("%s", buf)without a width specifier — unbounded read - Flag
strlen()result used as a signed integer — potential truncation on 64-bit - Flag user-controlled data used as a format string (
printf(user_input)) — format string attack - Flag integer arithmetic used as array index without bounds check
- Flag signed integer overflow — undefined behavior in C
Async / Concurrency
- Flag shared global or
staticvariables accessed from multiple threads without a mutex or_Atomic - Flag
pthread_mutex_t/sem_tnot initialized before use - Flag signal handlers that call non-async-signal-safe functions (
malloc,printf, etc.) - Flag
volatileused as a substitute for proper synchronization — it is not sufficient - Flag lock acquisition order inconsistency across call sites — deadlock risk
Resource Management
- Flag every
malloc/calloc/reallocpath — verify a matchingfreeexists on all exit paths - Flag
fopenwithout a matchingfcloseon all paths including error paths - Flag
dup/socket/openfile descriptors not closed on all paths - Flag stack-allocated VLAs (variable-length arrays) of unbounded size — stack overflow risk
- Flag
reallocreturn value assigned directly to the source pointer — leaks on failure
Exception Handling
- Flag ignored return values from
malloc,fopen,read,write,close— all can fail - Flag
errnochecked after a function that doesn't set it, or not checked immediately after one that does - Flag
perror/strerroras the sole error handling in library code — propagate errors to callers - Flag functions that return
-1on error without documenting whicherrnovalues are possible - Flag
assert()used for runtime error handling — disabled byNDEBUGin production builds
Performance
- Flag
strlen()called repeatedly on the same string in a loop — cache the result - Flag unnecessary copies of large structs passed by value — pass by pointer
- Flag
memcpy/memseton overlapping regions — usememmovefor overlapping - Flag repeated heap allocations in a tight loop — consider a pool or stack allocation
- Flag
volatileon variables not accessed by hardware or signal handlers — prevents optimization
Idioms and Best Practices
Memory Safety
- Every pointer must have a clear owner responsible for freeing it — document ownership in comments
- Set pointers to
NULLimmediately afterfreeto catch use-after-free early - Prefer
callocovermalloc+memsetfor zero-initialized allocations - Use
conston pointer parameters that the function does not modify
Defensive Coding
- Always check
NULLreturns from allocation functions - Use
size_tfor sizes and counts — neverint - Prefer
snprintfandfgetsover any unbounded string function - Compile with
-Wall -Wextra -Werrorand treat warnings as errors
Portability
- Do not assume pointer size equals
intsize — useintptr_t/uintptr_t - Do not rely on undefined behavior for performance — use compiler intrinsics instead
- Use
stdint.htypes (uint32_t,int64_t) for fixed-width requirements
Author: alirezarezvani, Alireza Rezvani skills and agents. License: MIT.
Shown here with formatting for the web. Keep the author and license notice when you reuse it.
Related entries
There are more like this in the members library
The Codex library holds 7,009 entries. Members can search all of them by meaning and read every one in full.