memory-api-misuse
Skill20 starsChanged 4 months agoMITView on GitHub
Detects function-local misuse of memory and resource APIs in C, C++, and Rust unsafe — allocations whose return value is not checked, frees on error paths that race the success path, locks initialized incorrectly, file descriptors leaked across exec. Use when writing or modifying C or C++ code that calls malloc/calloc/realloc/free, mmap/munmap, pthread_mutex_*, fopen/open, or any kernel/library memory or resource primitive. Use when writing Rust code inside an unsafe block that calls a raw allocation or pointer API.
In this repository52 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- authentication-failuresSkill
- broken-access-controlSkill
- concurrency-correctnessSkill
- contract-reviewSkill
- cryptographic-failuresSkill
- crypto-library-misuseSkill
- csrfSkill
- exceptional-conditionsSkill
- excessive-agencySkill
- file-uploadSkill
- graphql-securitySkill
- hardcoded-secretsSkill
- header-injectionSkill
- hotspotsSkill
- injectionSkill
- insecure-designSkill
- insecure-local-storageSkill
- insecure-output-handlingSkill
- insecure-plugin-designSkill
- integrity-failuresSkill
- ipc-securitySkill
- llm-supply-chainSkill
- logging-failuresSkill
- mass-assignmentSkill
- mcp-securitySkill
- model-dosSkill
- model-theftSkill
- multi-agent-trustSkill
- nosql-injectionSkill
- numeric-trust-boundarySkill
- oauth-implementationSkill
- open-redirectSkill
- overrelianceSkill
- path-traversalSkill
- privilege-handlingSkill
- prompt-injectionSkill
- prototype-pollutionSkill
- pr-reviewSkill
- race-conditionSkill
- rag-securitySkill
- redosSkill
- security-cleanupSkill
- security-misconfigurationSkill
- security-reviewSkill
- sensitive-disclosureSkill
- ssrfSkill
- supply-chainSkill
- threat-modelSkill
- token-smugglingSkill
- training-data-poisoningSkill
- unsafe-api-consumptionSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 38
Allocation cleanup has a single owner per path: Use one cleanup label, one RAII wrapper, or one defer/scope_exit — never free the same …
- Very specific: line 45
Use the close-on-exec mode flag at open time, not a follow-up fcntl race.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Memory API Misuse (CWE-690, CWE-415, CWE-401)
What this checks
C, C++, and Rust unsafe memory and resource APIs called incorrectly at a single call
site — a NULL return dereferenced, a double-free on an error path, a lock used
before init, a file descriptor that survives exec(). Existing static analyzers
catch the easy cases; this skill targets the ones that read clean on first pass but
fail in error or cleanup paths. It does local pattern matching at the call site,
not whole-program lifetime analysis (that's the job of TSAN, ASAN, Valgrind, and the
Rust borrow checker).
Vulnerable patterns
- Allocation API (
malloc,calloc,mmap, equivalent) whose return is used without a NULL orMAP_FAILEDcheck before the next dereference reallocwhose return value overwrites the original pointer in place, leaking the original allocation if reallocation fails- A buffer freed on an error path that also flows through a common cleanup label that frees it again, producing a double-free
- Mutex, rwlock, or semaphore used before its initializer (function call or static initializer macro) has run
- File-descriptor-producing call (
fopen,open, equivalent) that opens an fd which must not surviveexec()without the close-on-exec mode or flag set - Rust
unsafeblock that calls a raw FFI allocation or pointer API and dereferences the result with no NULL check free(or equivalent) on a pointer with no immediate nulling, leaving the dangling pointer available for later use-after-free
Fix immediately
Flag the vulnerable call site and rewrite it so the API return value is checked and cleanup runs exactly once per allocation. Establish these properties:
- Every allocation return is checked before use. A NULL or
MAP_FAILEDreturn turns into a controlled error path — never a silent dereference. reallocresults land in a temporary first. The original pointer remains valid on failure so the caller can free it deterministically; the original pointer is overwritten only after success is confirmed.- Allocation cleanup has a single owner per path. Use one cleanup label, one
RAII wrapper, or one
defer/scope_exit— neverfreethe same allocation on both an error branch and a shared cleanup path. - Synchronization primitives are initialized before first use. A static initializer macro at declaration or a documented init call before the first lock — never used uninitialized.
- File descriptors that should not cross
exec()are opened close-on-exec. Use the close-on-exec mode flag at open time, not a follow-upfcntlrace. - Freed pointers are nulled at the same call site so subsequent reads fault loudly instead of silently using freed memory.
Translate these principles to the specific allocator, lock, and fd API of the audited file. Use the platform's documented safe forms — do not invent variants.
Verification
After rewriting, confirm:
- Every allocation return is checked against NULL (or the API's documented failure sentinel) before use
- No allocation is freed on more than one path through the function — a single cleanup owner per allocation
- Every synchronization primitive is initialized before its first lock or wait call
- Every file descriptor that must not survive
exec()is opened with the close-on-exec flag or mode at open time - In Rust
unsafe, every raw pointer obtained from an FFI API is checked for NULL before dereference or slice construction
References
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

