Skip to main content

Lesson 5 of 8

Memory Safety Audit & const Correctness Checklist

Before shipping a C project, perform a systematic memory safety and const correctness audit. This section provides the full checklist with before/after examples for common issues found in real C codebases.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Production Safety Checklist - Audit Every Function

Production Safety Checklist - Audit Every Function
/* Memory Safety & const-Correctness Production Audit

═══════════════════════════════════════════════════════
CONST CORRECTNESS CHECKLIST
═══════════════════════════════════════════════════════

☐ Every function that doesn't modify its input
  uses const pointers:

  BAD:  void print_task(Task *t)
  GOOD: void print_task(const Task *t)

  BAD:  int find_by_id(TaskList *tl, uint32_t id)
  GOOD: Task *find_by_id(const TaskList *tl, uint32_t id)

☐ String parameters that are read-only:

  BAD:  int store_load(char *path, ...)
  GOOD: int store_load(const char *path, ...)

☐ Enum/struct parameters passed by value for small types:

  OK: void set_priority(Task *t, Priority p)  // p is by value


═══════════════════════════════════════════════════════
MEMORY MANAGEMENT CHECKLIST
═══════════════════════════════════════════════════════

☐ Every malloc/calloc has a NULL check?
  if (!ptr) { perror("..."); goto cleanup; }

☐ Every allocation has exactly one matching free?

☐ free() followed immediately by = NULL?
  free(tl->data); tl->data = NULL;

☐ No double-free?
  free(NULL) is safe - use it.

☐ realloc uses a temp pointer?
  void *tmp = realloc(p, n);
  if (!tmp) { free(p); return -1; }
  p = tmp;

☐ No return without freeing all allocations?
  → Use goto cleanup pattern.

☐ No stack arrays larger than ~64KB?
  → Use malloc for big buffers.

☐ No returning pointer to local variable?
  BAD: int *f(void) { int x = 5; return &x; }


═══════════════════════════════════════════════════════
STRING SAFETY CHECKLIST
═══════════════════════════════════════════════════════

☐ All buffer writes use size-limited functions?
  strncpy(dst, src, sizeof(dst)-1); dst[sizeof(dst)-1] = '';
  snprintf(buf, sizeof(buf), fmt, ...);
  fgets(buf, sizeof(buf), fp);

☐ No gets(), no scanf("%s", ...) without width?
  BAD:  scanf("%s", buf)
  GOOD: scanf("%63s", buf)  // 63 = sizeof(buf)-1

☐ String comparisons use strcmp, not ==?

☐ strtok not called on string literals?


═══════════════════════════════════════════════════════
FILE I/O CHECKLIST
═══════════════════════════════════════════════════════

☐ Every fopen return checked for NULL?
☐ Every fclose called exactly once per fopen?
☐ fwrite return value checked == expected count?
☐ Binary mode ("rb"/"wb") for struct data?
☐ Error paths free all allocations (goto cleanup)?


═══════════════════════════════════════════════════════
FINAL VERIFICATION COMMANDS
═══════════════════════════════════════════════════════

# 1. Clean build with strict warnings:
make clean && make CFLAGS="-Wall -Wextra -Wpedantic -std=c99 -O2"

# 2. ASAN + UBSan build and test:
make clean && make DEBUG=1
./taskman add "Test task"
./taskman list
./taskman done 1

# 3. Valgrind full check:
make clean && make
valgrind --leak-check=full --error-exitcode=1 ./taskman list

# 4. Static analysis (if available):
cppcheck --enable=all --std=c99 src/*.c

All four must pass with zero errors before shipping.       */

Common Mistakes

  • Auditing only the happy path - check every `return` in every function to make sure allocations made before that return are freed. Error paths are the most common source of leaks.
  • Not running ASAN on the complete command flow - run every subcommand (`add`, `list`, `done`, `delete`, `stats`) under ASAN. Bugs often only appear on specific code paths.
  • Trusting `-Wall` to find everything - `-Wall` misses many UB issues (signed overflow, type aliasing, unsequenced modification). Always combine with `-fsanitize=undefined` for full coverage.

Tip

Tip

Practice Memory Safety Audit const Correctness Checklist in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Rigorous memory auditing ensures zero leaks, valid pointers, and robust error returns

Practice Task

Note

Practice Task - (1) Write a working example of Memory Safety Audit const Correctness Checklist from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Memory Safety Audit const Correctness Checklist is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • Before shipping a C project, perform a systematic memory safety and const correctness audit.
  • Auditing only the happy path - check every `return` in every function to make sure allocations made before that return are freed. Error paths are the most common source of leaks.
  • Not running ASAN on the complete command flow - run every subcommand (`add`, `list`, `done`, `delete`, `stats`) under ASAN. Bugs often only appear on specific code paths.
  • Trusting `-Wall` to find everything - `-Wall` misses many UB issues (signed overflow, type aliasing, unsequenced modification). Always combine with `-fsanitize=undefined` for full coverage.

Topics in This Module

0/8 done