Fähigkeiten memory-safety-patterns
🛡️

memory-safety-patterns

Sicher

Implement Memory Safety Patterns

Auch verfügbar von: wshobson

Write crash-free, secure code using proven memory safety patterns across Rust, C++, and C. Prevent leaks, use-after-free, and buffer overflows with RAII, ownership models, and smart pointers.

Unterstützt: Claude Codex Code(CC)
🥉 75 Bronze
1

Die Skill-ZIP herunterladen

2

In Claude hochladen

Gehe zu Einstellungen → Fähigkeiten → Skills → Skill hochladen

3

Einschalten und loslegen

Teste es

Verwendung von "memory-safety-patterns". How do I prevent memory leaks when opening files in C++?

Erwartetes Ergebnis:

Use RAII with a smart file wrapper class or std::fstream. The file automatically closes when the object is destroyed, even if an exception occurs. Example: std::fstream file("data.txt"); - destructor handles cleanup automatically.

Verwendung von "memory-safety-patterns". What is the difference between Box, Rc, and Arc in Rust?

Erwartetes Ergebnis:

Box<T> for single ownership with heap allocation. Rc<T> for shared ownership (single-threaded) - increments reference count on clone. Arc<T> for atomic shared ownership across threads - thread-safe reference counting.

Verwendung von "memory-safety-patterns". How do I handle bounds checking safely?

Erwartetes Ergebnis:

In C++, use std::vector::at() which throws std::out_of_range, or std::span for array views. In Rust, indexing with [] panics on out-of-bounds, while .get() returns Option for safe handling. Prefer iterators to avoid manual bounds checking.

Sicherheitsaudit

Sicher
v1 • 2/25/2026

All 56 static findings are false positives. The skill contains only documentation and code examples for memory safety patterns. Thread spawning (thread::spawn) was misidentified as process spawning. Markdown backticks for code formatting were misidentified as shell execution. Documentation URLs and debugging tool references are legitimate educational content.

2
Gescannte Dateien
640
Analysierte Zeilen
0
befunde
1
Gesamtzahl Audits
Keine Sicherheitsprobleme gefunden
Auditiert von: claude

Qualitätsbewertung

38
Architektur
100
Wartbarkeit
87
Inhalt
50
Community
100
Sicherheit
100
Spezifikationskonformität

Was du bauen kannst

Systems Programming

Write operating systems, drivers, or embedded software that is free from memory corruption vulnerabilities.

Security-Critical Applications

Build applications where memory safety is mandatory: cryptography, network services, file processing.

Legacy Code Modernization

Apply memory safety patterns when refactoring C/C++ codebases to reduce bugs and improve reliability.

Probiere diese Prompts

Basic RAII Implementation
Show me how to implement a RAII pattern in C++ for a file handle class. Include proper copy/move semantics and ensure the file is automatically closed when the object goes out of scope.
Rust Ownership Example
Explain how Rust ownership prevents use-after-free bugs. Show an example with a struct that holds a reference, including lifetime annotations and when to use Box, Rc, and Arc.
C Resource Management
Write a C function that opens a file, allocates memory, and processes data. Use the goto-cleanup pattern to ensure all resources are properly freed on error.
Thread-Safe Counter
Implement a thread-safe counter in both C++ using atomics and mutex, and Rust using Arc and Mutex. Show how each language prevents data races.

Bewährte Verfahren

  • Prefer RAII and smart pointers over manual memory management to ensure automatic cleanup
  • Use the borrow checker in Rust rather than fighting it - it prevents real bugs
  • Run AddressSanitizer and Valgrind during development to catch memory issues early

Vermeiden

  • Using raw pointers in C++ instead of smart pointers - leads to leaks and dangling pointers
  • Ignoring compiler warnings about dangling references - they indicate real bugs
  • Using unsafe blocks in Rust without documenting why and isolating them from safe code

Häufig gestellte Fragen

What is RAII and why does it matter?
RAII (Resource Acquisition Is Initialization) ties resource lifetime to object lifetime. When the object goes out of scope, its destructor runs and releases resources. This prevents leaks and ensures cleanup even when exceptions occur.
How does Rust prevent memory bugs?
Rust uses an ownership system where each value has exactly one owner. When the owner goes out of scope, the value is dropped. The borrow checker enforces these rules at compile time, preventing use-after-free, double-free, and data races.
When should I use smart pointers in C++?
Use unique_ptr for single ownership (default choice). Use shared_ptr when multiple parts of code need to own the same object. Use weak_ptr to break cycles in shared_ptr graphs. Avoid raw pointers unless interfacing with C code.
What is the goto-cleanup pattern in C?
A pattern where all resource allocations happen at the start, and a single cleanup label frees everything in reverse order. This ensures resources are freed even when errors occur at any point in the function, avoiding leaks.
How do I choose between Rust and C++ for memory safety?
Rust provides stronger compile-time guarantees through ownership and borrowing. C++ requires more discipline but gives more control. Choose Rust for new projects where memory safety is critical. Use C++ when interfacing with existing C++ codebases or when maximum performance is needed.
What debugging tools help catch memory issues?
AddressSanitizer (ASan) catches use-after-free, leaks, and buffer overflows. Valgrind detects memory errors in C/C++. Rust Miri detects undefined behavior. ThreadSanitizer finds data races. Use these tools during development and in CI pipelines.