this post was submitted on 16 Mar 2025
798 points (98.2% liked)

Programmer Humor

21896 readers
2438 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
798
C++ (ani.social)
submitted 1 week ago* (last edited 1 week ago) by kiri@ani.social to c/programmer_humor@programming.dev
 
you are viewing a single comment's thread
view the rest of the comments
[–] RoyaltyInTraining@lemmy.world 5 points 1 week ago (1 children)

The worst thing you can do in non-unsafe Rust is perform an out-of-bounds indexing operation, or anything else that panics. The error you get tells you the panic's exact location in the source code, down to the line and column. Meanwhile, C and C++ either don't produce an error at all when accessing uninitialized memory (which is arguably the worst behavior), or it segfaults with zero extra info.

The only way to make Rust segfault is by performing unsafe operations, and those must always be clearly marked.

[–] weker01@sh.itjust.works 4 points 1 week ago* (last edited 1 week ago) (1 children)

The only way to make Rust segfault is by performing unsafe operations.

Challange accepted. The following Rust code technically segfaults:

fn stackover(a : i64) -> i64 {
    return stackover(a);
}


fn main() {
    println!("{}", stackover(100));
}

A stack overflow is technically a segmentation violation. At least on linux the program recives the SIGSEGV signal. This compiles and I am no rust dev but this does not use unsafe code, right?

While the compiler shows a warning, the error message the program prints when run is not very helpfull IMHO:

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
[1]    45211 IOT instruction (core dumped)  ../target/debug/rust

Edit: Even the compiler warning can be tricked by making it do recusion in pairs:

fn stackover_a(a : i64) -> i64 {
    return stackover_b(a);
}

fn stackover_b(a : i64) -> i64 {
    return stackover_a(a);
}

fn main() {
    println!("{}", stackover_a(100));
}