Compétences memory-safety-patterns
🛡️

memory-safety-patterns

Sûr

Implement Memory Safety Patterns

Également disponible depuis: 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.

Prend en charge: Claude Codex Code(CC)
🥉 75 Bronze
1

Télécharger le ZIP du skill

2

Importer dans Claude

Allez dans Paramètres → Capacités → Skills → Importer un skill

3

Activez et commencez à utiliser

Tester

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

Résultat attendu:

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.

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

Résultat attendu:

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.

Utilisation de "memory-safety-patterns". How do I handle bounds checking safely?

Résultat attendu:

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.

Audit de sécurité

Sûr
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
Fichiers analysés
640
Lignes analysées
0
résultats
1
Total des audits
Aucun problème de sécurité trouvé
Audité par: claude

Score de qualité

38
Architecture
100
Maintenabilité
87
Contenu
50
Communauté
100
Sécurité
100
Conformité aux spécifications

Ce que vous pouvez construire

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.

Essayez ces 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.

Bonnes pratiques

  • 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

Éviter

  • 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

Foire aux questions

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.

Détails du développeur

Structure de fichiers