Index of every page on this site: llms.txt
All skills
wshobson avatar

/memory-safety-patterns

@4220308
by Seth Hobsonwshobson/agents43k stars
4,518
Run it yourself

Implement memory-safe programming with RAII, ownership, smart pointers, and resource management across Rust, C++, and C. Use when writing safe systems code, managing resources, or preventing memory bugs.

Use this Skill: https://skilld.dev/gh/wshobson/agents/memory-safety-patterns

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

SKILL.md

≈57 tokens for metadata: the name and description. ≈583 when used: this file. ≈2.8k more on demand in 1 file.

Description uses 2.9% of example budget

Before choosing a Skill, your Agent reads its name and description. All available Skills share that space.

  • A shorter description leaves more room for other Skills. This entry exceeds our 1% size suggestion.

In our Claude Code example, all Skill names and descriptions share 8,000 characters. This Skill uses ≈228 characters, or 2.9%.

The 1% threshold is a size suggestion. Longer descriptions can still fit.

Your model, settings, and other Skills decide how much text your Agent can read.

Example settings and source

The example uses a 200k-token context and default Claude Code settings. The count includes the name, description, separators, and when_to_use when present. Codex also counts local file paths.

Skit's source and limits: Codex 0.160.1, Claude Code 2.1.292.

Memory Safety Patterns

Cross-language patterns for memory-safe programming including RAII, ownership, smart pointers, and resource management.

When to Use This Skill

  • Writing memory-safe systems code
  • Managing resources (files, sockets, memory)
  • Preventing use-after-free and leaks
  • Implementing RAII patterns
  • Choosing between languages for safety
  • Debugging memory issues

Core Concepts

1. Memory Bug Categories

Bug Type Description Prevention
Use-after-free Access freed memory Ownership, RAII
Double-free Free same memory twice Smart pointers
Memory leak Never free memory RAII, GC
Buffer overflow Write past buffer end Bounds checking
Dangling pointer Pointer to freed memory Lifetime tracking
Data race Concurrent unsynchronized access Ownership, Sync

2. Safety Spectrum

Manual (C) → Smart Pointers (C++) → Ownership (Rust) → GC (Go, Java)
Less safe                                              More safe
More control                                           Less control

Detailed patterns and worked examples

Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.

Best Practices

Do's

  • Prefer RAII - Tie resource lifetime to scope
  • Use smart pointers - Avoid raw pointers in C++
  • Understand ownership - Know who owns what
  • Check bounds - Use safe access methods
  • Use tools - AddressSanitizer, Valgrind, Miri

Don'ts

  • Don't use raw pointers - Unless interfacing with C
  • Don't return local references - Dangling pointer
  • Don't ignore compiler warnings - They catch bugs
  • Don't use unsafe carelessly - In Rust, minimize it
  • Don't assume thread safety - Be explicit

Debugging Tools

# AddressSanitizer (Clang/GCC)
clang++ -fsanitize=address -g source.cpp

# Valgrind
valgrind --leak-check=full ./program

# Rust Miri (undefined behavior detector)
cargo +nightly miri run

# ThreadSanitizer
clang++ -fsanitize=thread -g source.cpp

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts21d5 checks · Risk SAFE
  • Gen Agent Trust Hub21d

    This skill provides comprehensive documentation and code examples for implementing memory safety patterns across C, C++, and Rust. It focuses on defensive programming techniques like RAII, ownership, and smart pointers to prevent common security vulnerabilities.

  • Socket23d

    No alerts

  • Snyk23d

    Risk: LOW · No issues

  • Runlayer8mo

    1 file scanned · No issues

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 4220308. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 3 days ago.

Activeupdated 6 months ago
  • Rust
  • c++
  • c
  • memory-safety
  • raii
  • ownership
  • smart-pointers
  • systems-programming
  • resource-management

README badge

README badge for wshobson/agents/memory-safety-patterns

Teaches memory-safety patterns including RAII, ownership models, smart pointers, and resource management across Rust, C++, and C. Use when writing systems code, managing file/socket resources, or preventing use-after-free and memory leaks.

Generated from the current SKILL.md.

Does this skill cover all three languages equally?
The skill covers Rust, C++, and C with patterns applicable across all three. Rust has the strongest memory safety guarantees through ownership; C++ relies on smart pointers and RAII; C requires manual management.
What specific memory bugs does this address?
The skill covers use-after-free, double-free, memory leaks, buffer overflow, dangling pointers, and data races. Each has dedicated prevention strategies.
Are there code examples in the skill?
The skill references detailed patterns and worked examples in a `references/details.md` file. The main SKILL.md provides conceptual guidance, best practices, and debugging tool commands.
Does this include concurrency and thread safety?
Yes. The skill covers data races and concurrent access as part of memory safety, with Rust's ownership model and ThreadSanitizer mentioned for detection.
What debugging tools does this recommend?
The skill includes AddressSanitizer, Valgrind, Rust Miri, and ThreadSanitizer with command examples for Clang, GCC, and Rust.

Generated from the current SKILL.md. These answers refresh after source changes.