# C Code Review Review {{input}} for correctness, safety, and best practices. ## Memory Safety - Every `malloc`/`calloc`/`realloc` return value must be checked for NULL - Every allocation must have a corresponding `free`; trace ownership through all code paths - No buffer overflows: check array bounds before indexing; use `snprintf` not `sprintf` - No use-after-free or double-free; set pointers to NULL after freeing - No stack-allocated VLAs for untrusted sizes — use heap allocation with bounds checks ## Undefined Behaviour - No signed integer overflow (use unsigned or check before arithmetic) - No null pointer dereference — validate pointers before use - No out-of-bounds reads/writes — validate indices against array size - No uninitialized reads — initialise all variables at declaration - Avoid relying on implementation-defined behaviour (e.g. right-shifting negative integers) ## String and I/O - Use `strnlen`, `strncpy`, `strncat` — never the unbounded variants - Validate `scanf`/`sscanf` field widths; check return values - Sanitise all format strings passed to `printf`-family functions — never pass user data as format ## Error Handling - Check the return value of every system call and library function - Return meaningful error codes or set `errno`; do not silently ignore failures - Clean up resources (files, sockets, memory) on every error path ## Code Quality - Functions should do one thing; < 50 lines each - Use `const` for pointer parameters that are not modified - No global mutable state unless absolutely necessary; document it explicitly - Compile with `-Wall -Wextra -Werror` and fix all warnings --- List findings as **Critical** (memory unsafety/UB/security) -> **Major** (bugs/resource leaks/unchecked errors) -> **Minor** (style/clarity). Cite each location and suggest the concrete fix.