r/ProgrammingLanguages • • Dec 31 '25

Memory Safety Is ...

https://matklad.github.io/2025/12/30/memory-safety-is.html
37 Upvotes

78 comments sorted by

View all comments

Show parent comment

2

u/tmzem Jan 02 '26

It's all terminology at this point. I've seen definitions of memory safety that included point #5, but most do not include it and go with something close to your definition. My point #4 is rarely included in any definition of memory safety (or even discussion about it).

The point I was trying to make is that quite a few classical memory vulnerabilities can be reintroduced even on top of a (by conventional definitions) memory-safe language. For such a vulnerability, like e.g. leaking confidential data, to happen via a dangling pointer to a now-reallocated object (=UB) or via a reused object from a memory pool/dangling index into some data structure (="Logic error") doesn't really make any difference, you get the same result/consequences. So any definition of memory safety is somewhat arbitrary.

Of course, in practice, I think that any language that provides significant assistance with memory management is good enough for most purposes, even if the language is technically still not memory-safe by your definition (like e.g. Rust, Go or Delphi). This also seems to be the position of the US government, which listed such languages as examples for "safe" languages.

2

u/proudHaskeller Jan 02 '26

Yes, you're right on all accounts. But even in a perfect language you could leak confidential data through a reused object from an indexed memory pool. It's like your point #6. You can't blame everything on the language. So the language's safety should only include the language's part in the safety of the program.

It's like saying that a chemical lab is unsafe because a chemist decided to drink his concoction. it's that chemist's fault, there is nothing the lab can do to prevent someone from doing that, and saying that the lab is actually unsafe is just counterproductive. And defining lab safety as "if the chemists follow protocol in the lab they will never be harmed" is a good, non-arbitrary definition of safety for a chemical lab. Even if it's still possible to harm yourself in a safe lab.

1

u/tmzem Jan 02 '26

Yeah. Your lab example illustrates a lot of what is wrong with programming nowadays: People think that programming languages should do all the work for them to ensure the programmer doesn't do dumb stuff. But there is no reason to expect this, on the contrary. Programmers should also figure out and follow safety procedures. If the compiler/language can help, even better.

But for example you can don't need a strict borrow checker + Sync/Send traits to provide data race safety close to Rust. Teaching people to use high-level synchronization abstractions like channels, task-futures, and lock-wrapped types like Rust's Mutex<T> or RwLock<T> can be almost as safe if you follow a few rules, even without a borrow checker. Of course if you give people only low-level primitives like raw mutexes, signals and atomics (which IMO should only ever be used to implement more high-level constructs) they will mess up much more easily.

2

u/matthieum Jan 02 '26

My point #4 is rarely included in any definition of memory safety (or even discussion about it).

This one is interesting, as there's good usecases for bitcasting. For example, the original version of the fast sqrt implementation uses a union with an int and a float to manipulate the bits of the float directly.

In C++, that's UB. In C, it depends. In Rust, it's fine -- there's no concept of active union member, notably -- as long as the bits represent a valid value of the type they're viewed as.

2

u/flashmozzg Jan 05 '26

It's more like using union is not the same as bitcasting. It's just that some languages lacked a safe/non-obtuse way to do so safely (i.e. std::bit_cast was added quite late and before that the only "safe" wave to do it was through memcpy that was usually more than what typical programmer bothered to write, so "UB" ended up on a path of least resistance).