| Crates.io | rust-memory-safety-examples |
| lib.rs | rust-memory-safety-examples |
| version | 2.0.0 |
| created_at | 2025-11-18 05:27:34.938816+00 |
| updated_at | 2025-12-11 08:20:28.517465+00 |
| description | Comprehensive educational examples demonstrating memory-safe programming patterns with CVE case studies and benchmarks |
| homepage | |
| repository | https://github.com/guardsarm/rust-memory-safety-examples |
| max_upload_size | |
| id | 1937916 |
| size | 117,070 |
Educational examples demonstrating memory-safe programming patterns in Rust for financial systems and critical infrastructure. This repository provides clear, documented examples of how Rust eliminates entire classes of security vulnerabilities.
This project demonstrates memory safety concepts for:
These examples directly address 2024 CISA/FBI guidance recommending memory-safe languages for critical infrastructure. Demonstrates how Rust eliminates 70% of security vulnerabilities found in C/C++ systems.
buffer_overflow moduleuse_after_free moduledata_race moduleinteger_overflow modulenull_pointer module# Clone repository
git clone https://github.com/guardsarm/rust-memory-safety-examples
cd rust-memory-safety-examples
# Run all examples
cargo test -- --nocapture
# Run specific examples
cargo run --example buffer_overflow_prevention
cargo run --example use_after_free_prevention
cargo run --example data_race_prevention
use rust_memory_safety_examples::buffer_overflow;
// C/C++ code (UNSAFE):
// char buffer[10];
// strcpy(buffer, "This is way too long"); // Buffer overflow!
// Rust equivalent (SAFE):
let data = vec![1, 2, 3, 4, 5];
if let Some(&value) = data.get(10) {
println!("Value: {}", value);
} else {
println!("Index out of bounds - safely handled!");
}
use rust_memory_safety_examples::use_after_free;
// C/C++ code (UNSAFE):
// int* ptr;
// {
// int x = 42;
// ptr = &x;
// }
// printf("%d", *ptr); // Use-after-free!
// Rust equivalent (SAFE - won't compile):
// let ptr: &i32;
// {
// let x = 42;
// ptr = &x; // ERROR: `x` does not live long enough
// }
use rust_memory_safety_examples::data_race;
use std::sync::{Arc, Mutex};
// Safe concurrent access
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter_clone = Arc::clone(&counter);
let handle = std::thread::spawn(move || {
let mut num = counter_clone.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
// No data races possible!
Common vulnerabilities that cannot occur in safe Rust:
Microsoft research shows:
# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run specific module tests
cargo test buffer_overflow
cargo test use_after_free
cargo test data_race
# Generate and open documentation
cargo doc --open
MIT License - See LICENSE file
Tony Chuks Awunor
Contributions welcome! Please open an issue or pull request with:
If you use these examples in educational or research contexts, please cite:
Awunor, T.C. (2024). Rust Memory Safety Examples: Educational Demonstrations
of Memory-Safe Programming. https://github.com/guardsarm/rust-memory-safety-examples
Educational. Practical. Memory-safe. Implemented in Rust.