gets

Crates.iogets
lib.rsgets
version0.0.1
sourcesrc
created_at2023-09-06 12:20:44.071497
updated_at2023-09-06 12:20:44.071497
descriptionNever use this function
homepage
repository
max_upload_size
id965220
size5,178
(AcqRel)

documentation

README

gets

Pure Rust implementation of the function in the C standard that is impossible to use without introducing security vulnerabilities.

Example usage

// Stupid warnings! I've programmed for years, I know what I'm doing!
#![allow(warnings)]

// Serde did this, so it's fine, right?
let mut buf: [c_char; 128] = std::mem::uninitialized();

println!("What is your name?");

// Surely nobody would ever input anything longer than 127 bytes?
let ptr = gets(buf.as_mut_ptr());

if ptr.is_null() {
    return;
}

// Surely nobody would ever input invalid UTF-8?
let name = CStr::from_ptr(ptr).to_str().unwrap_unchecked();

println!("Hello, {}!", name);
$ cargo run --example basic
   Compiling gets v0.0.1 (/home/idiot/gets)
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/examples/basic`
What is your name?
I don't know buffer overflows are a thing
Hello, I don't know buffer overflows are a thing!

$ This seems to be fine, let's run it in production, what could go wrong??
This: command not found

$ cargo run --example basic --release
   Compiling gets v0.0.1 (/home/idiot/gets)
    Finished release [optimized] target(s) in 0.13s
     Running `target/release/examples/basic`
What is your name?
Charles the Third, by the Grace of God of the United Kingdom of Great Britain and Northern Ireland and of His other Realms and Territories King, Head of the Commonwealth, Defender of the Faith
Hello, Charles the Third, by the Grace of God of the United Kingdom of Great Britain and Northern Ireland and of His other Realms and Territories King, Head of the Commonwealth, Defender of the Faith!
Segmentation fault (core dumped)

$ Oh no, it crashed. Maybe I should use location services to increase the buffer size if the user is in Buckingham Palace?
Oh: command not found
Commit count: 0

cargo fmt