r/rustjerk • • Oct 24 '26

This is fun

Post image
use std::alloc::{alloc, dealloc, Layout};
use std::slice;

fn main() {
    [0].into_iter()
        .chain(
            (0..6).into_iter().map(|i| 1 << i)
        ).for_each(unsafe_shenanigans)
}

fn unsafe_shenanigans(length: usize) {
    unsafe {
        let layout: Layout = Layout::array::<i32>(length).unwrap();
        let pointer: *mut i32 = alloc(layout).cast();

        let arr: &mut [i32] = slice::from_raw_parts_mut(pointer, length);
        arr.fill(1);

        println!("{:?}", arr);

        dealloc(pointer.cast(), layout);
    }
}

Output:

[]
[1]
[1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

When do I get my Rust software developer job?

117 Upvotes

15 comments sorted by

26

u/--San-- Oct 25 '26

Line 16 is UB, you can't create references to uninitialized data.

3

u/Schaex Oct 25 '26

I did write this just for fun, but now I am wondering. Why is line 16 undefined behavior?

I understand that the data in arr can potentially be garbage because I called alloc rather than alloc_zeroed, but I thought that I would always get a reference to a slice with size length.

Unless, by UB, you mean that the slice returned by slice::from_raw_parts_mut may contain garbage data.

7

u/MalbaCato Oct 25 '26

let unjerk : !Jerk = {

yep, exactly. references shouldn't point to uninitialised data.

it's even more complicated than that however - in current rustc this is not UB, and there's an idea to define it as not UB, but the opsem-wg is not yet sure on that and hence this is one of those unspecified corners of rust. in any case, undefining this will require a bunch of language, stdlib and ecosystem changes, so it's not happening any time soon.

}

unsafe rust is hard. even the answer to whether your program has UB is undefined. just write c man, then your program definitely has undefined behaviour, and you just have to find out why.

1

u/--San-- Oct 25 '26

By UB I mean that it isn't valid rust, anything can happen, including nothing.

You are giving the compiler invalid code, and the compiler is free to assume that you will never do that (and it does assume it). Meaning that no one can say what this program compiles to.

1

u/ItsEntDev 28d ago

/uj It is valid Rust, just debated Rust. Also, they were pretty clearly asking WHY it is UB not WHAT UB is.

29

u/--San-- Oct 25 '26

And I just now read the subreddit name...

13

u/BadRuiner Oct 25 '26

You outjerked jerk

61

u/steaming_quettle Oct 24 '26

You're getting a C job actually

24

u/The-Dark-Legion ®ü$t Føūñdåtīón Oct 25 '26

This hurts me because it should be passing in Miri.

6

u/afrolino02 Oct 25 '26

Can i get the font name?

10

u/SetKaung Oct 25 '26

It seems to me like OP is using Intellij software. So probably Jetbrain Mono

5

u/GirlInTheFirebrigade Oct 25 '26

I thought so too at first, but that’s not the correct zero. JetBrainsMono adds a dot in the zero to make it more distinguishable from an O.

Edit: the image was too small ln my phone to render the dot. it’s jetbrains mono

5

u/RoseboysHotAsf Oct 26 '26

Im a c++ programmer and see nothing wrong with this code

1

u/AdOnly69 Oct 26 '26

Bro probably just copied it from some production codebase

2

u/Snudget Oct 26 '26

No, you first have to use a bug in the compiler to turn this into blazingly fast memory safe segfault