Comment

Comment by ridiculous_fish

The author gives the following function:

    static BYTES: &[u8] = b"hello world";
    fn foo1(index: usize) -> u8 {
        BYTES[index]
    }

and then writes we can't make foo1 commit UB just by giving it a large index ...foo1 will never cause UB without the caller committing UB first.

But strictly speaking this is wrong! If I plug foo1(large_index) into `process::Command::pre_exec()`, then the resulting panic may invoke UB, without the caller having committed UB first.

So you have to decide whether your function is designed to be run within these environments. If it is, and you add an allocation, then that's your fault!

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton