safe_copy

pub fn safe_copy(dest: &mut [u8], src: &[u8]) -> Result<(), &'static str>
Expand description

VULNERABLE C CODE (for comparison):

#include <string.h>

void vulnerable_copy(char* dest, const char* src) {
    strcpy(dest, src);  // NO BOUNDS CHECKING!
}

int main() {
    char buffer[10];
    char* long_string = "This is a very long string that will overflow";
    vulnerable_copy(buffer, long_string);  // BUFFER OVERFLOW!
    return 0;
}

Vulnerability: strcpy doesn’t check destination buffer size, causing stack corruption, potential code execution, crashes.

CVE Examples:

  • CVE-2021-3156 (sudo): Heap buffer overflow
  • CVE-2014-0160 (Heartbleed): Buffer over-read

SAFE RUST EQUIVALENT: Rust prevents buffer overflows at compile time through bounds checking