Skip to content
Skills Library
Programming · Building & coding

c-memory-safety

/c-memory-safety

Write C with explicit ownership conventions, bounds discipline, and sanitizers wired into every test run. Use when writing or reviewing C code, or hunting memory corruption.

Repository
AI-SKILLS ↗
Licence
MIT
Last repository change
05.09.2026

Assessment and security

90/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content25 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 05.09.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Write C with explicit ownership conventions, bounds discipline, and sanitizers wired into every test run. Use when writing or reviewing C code, or hunting memory corruption.

Original author description

Write C with explicit ownership conventions, bounds discipline, and sanitizers wired into every test run. Use when writing or reviewing C code, or hunting memory corruption.

How it works

  1. Method
  2. Boundaries
Original SKILL.md headings · 61 lines, 441 words
SKILL.md preview
---
name: c-memory-safety
description: Write C with explicit ownership conventions, bounds discipline, and sanitizers wired into every test run. Use when writing or reviewing C code, or hunting memory corruption.
---

# C memory safety

C gives you no checker, so you build one from three parts: conventions
that make ownership visible, APIs that make bounds explicit, and
sanitizers that catch what convention missed. Teams that skip any leg
ship the corruption the other two would have caught.

## Method

1. **Declare ownership in names and comments.** Every
   pointer-returning or pointer-taking function states who frees:
   suffix conventions (`_create/_destroy`, `_take` for ownership
   transfer, `_borrow`/const for no transfer), a
   `/* caller owns result */` comment where names cannot carry it.
   Ambiguity here is where double-frees and leaks breed; the
   convention is your borrow checker.
2. **Pair every resource with one owner and one release path.**
   Allocate and free in the same module; single-exit cleanup with
   `goto err` chains (release in reverse order) keeps error paths
   from leaking; NULL-out pointers after free so use-after-free
   becomes a crash instead of silent reuse. One clear owner per
   allocation; shared ownership in C is a design smell to engineer
   away, not manage.
3. **Make every buffer carry its size.** Functions take
   `(buf, len)` pairs or a slice struct; strings use explicit-length
   APIs (`snprintf`, `strnlen`, memccpy-style) and ban the unbounded
   family (`gets`, `strcpy`, `sprintf`) via lint. Integer overflow
   in size math is the pre-corruption bug: check multiplications
   (`calloc` over `malloc(n*size)`), use size_t consistently, and
   validate untrusted lengths at the boundary (see input-validation).
4. **Run sanitizers as the default test build.**
   AddressSanitizer+UndefinedBehaviorSanitizer on every test and
   fuzz run (cheap, catches use-after-free, overflows, UB);
   MemorySanitizer or Valgrind for uninitialized reads where ASan is
   blind; ThreadSanitizer on concurrent code (see race-conditions).
   A sanitizer finding is a bug even when the test passes; CI treats
   it as failure (see fuzz-testing for the input side).
5. **Prefer arena and pool lifetimes for complex object graphs.**
   Allocating related objects from one arena freed wholesale
   converts N ownership decisions into one, eliminates leak classes,
   and often wins performance; per-request/per-phase arenas fit
   servers and parsers naturally.
6. **Harden the production build.** Fortify source, stack
   protectors, ASLR/PIE, RELRO as defaults; assertions on invariants
   kept in release for corruption-adjacent code (fail fast beats
   corrupt slowly; see defensive-programming). Crashes then arrive
   with dumps you can read (see core-dumps).

## Boundaries

- These practices reduce, not eliminate, memory unsafety; for new
  components with hostile input, weigh writing them in Rust and
  linking via FFI (see ffi-boundaries) as the stronger fix.
- Legacy code adopts this incrementally: sanitize tests first, add
  bounds at the trust boundary, refactor ownership as you touch
  functions, not in a big bang.
- Kernel, embedded, and allocator-free environments constrain the
  toolbox (no ASan on target); compensate with host-side unit tests
  under sanitizers and stricter review (see embedded-debugging).

Versions

The database has no other skills named c-memory-safety.

Database snapshot: 13.11.2026 · package version a31d94d0a144 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 95rust-ownership-system
    Use when Rust's ownership system including ownership rules, borrowing, lifetimes, and memory safety. Use when working with Rust memory management.
  • 90cc-native-memory-safety
    Use when C/C++ leaks, use-after-free, double release, bounds errors, allocator mismatch, FFI ownership or teardown races need diagnosis or repair.
  • 100c-systems
    C patterns for systems code, libraries, and native extensions: module layout, function decomposition, status-enum errors, memory safety, undefined behavior, and performance measurement. Use when writing, reviewing, refactoring, or debugging C, working with malloc lifetimes,…
  • 100code-style
    Language-agnostic code-design rules for explicit state, failure, ownership, and I/O boundaries. Use when implementing, refactoring, or reviewing code.
  • 100memory-safe-migration
    Guide secure migration of code from memory-unsafe languages (C, C++, Assembly) to memory-safe languages (Rust, Go, Java, C#, Swift). Use when migrating or rewriting legacy C/C++ code, designing FFI boundaries between safe and unsafe code, writing new modules in existing C/C++…
  • 100check-bounds-safety
    Apply type-safe bounds checking patterns using VPIndex/VPLength types instead of usize. Use when working with arrays, buffers, cursors, viewports, or any code that handles indices and lengths.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.