Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Memory leaks are bad, but they can't break type safety. Buffer overflows and use-after-free are categorically more dangerous.


sort by: page size:

That’s not correct. Memory safety consists of properties necessary for type safety to be upheld. Leaks alone can crash a program but can’t defeat type safety.

Memory leaks are not memory safety.

Memory leaks in all languages are safe, they just slow performance and cause crashes.

Rust still memory leaks.


leaks aren't unsafe though. Things like bounds checking and aliasing guarantees are like 90% of the memory safety issues you care about, and those can be performant.

Memory leaks are annoying and, yes, you can get them in memory safe languages.

But they are way less severe than memory corruption. Memory unsafe languages are liable to undefined behaviour, which is actively dangerous, both in theory and practice.


You have a point, but memory leaks generally aren't considered as problematic as other memory issues, such as buffer overruns and use after free. While they can cause programs to crash and degrade, they generally don't corrupt program data or lead to security vulnerabilities.

As such, people often don't include protection from memory leaks as a criterion for 'memory safety'. It's possible to explicitly leak memory in the safe subset of Rust (e.g. Box::leak). It's also possible to 'leak' memory in GC'd languages too by keeping old references around you'll never use, although this example is just arguing semantics even more.


I don't consider memory leaks a memory safety issue since all they do use use up memory and might cause failing allocations, which has to be handled one way or another regardless whenever you use dynamic memory.

There's different classes of memory un-safety: buffer overflow, use after free, and double free being the main ones. We haven't seen a mainstream language capable of preventing use and free and double free without GC overhead until Rust. And that's because figuring out when an object is genuinely not in use anymore, at compile time, is a really hard problem. But a buffer overflow like from the article? That's just a matter of saving the length of the array alongside the pointer and doing a bounds check, which a compiler could easily insert if your language had a native array type. Pascal and its descendants have been doing that for decades.

Why is this a nothingburger? If you're making the claim that it's safe and it actively leaks memory, how do we know it doesn't also allow buffer overruns or double frees?

It would be nice if you allowed an option to compile with memory tracking so that as a user of the language you can debug this sort of stuff to make sure it's not a big deal waiting to happen.


Memory leaks are not memory unsafe.

It's also funny because memory leaks literally aren't a memory safety issue. If they were, every garbage collected language would be memory-unsafe.

That is a part of memory safety, not just type safety. Type safety could fix this form of memory issue, yes.

That's not memory-unsafety. Memory-safety means avoiding bugs like buffer overflow, ROP, etc.

Memory leaks can happen in safe code, segfaults cannot. That’s at least one major difference.

Memory leaks aren’t unsafe fwiw.

unsafe memory and unsafe types are not okay if you widely claim to be memory safe. Thousands of people actually believe these lies, and companies do makes decisions on these claims. It's much more memory unsafe than Java in fact. Java recently removed the unsafe tricks, whilst rust was once safe and went more and more unsafe since.

Have to say that nothing can make your program automatically safe (defined by common sense, not "memory leaks are also memory safe"[0]) without any effort, and the trend to claim some languages are inherently "safe" while others are not is definitely a hype. Modern C++ plus static analyzing (Chromium rolled out their own Clang plugins to do that) provides better flexibility.

[0]: http://huonw.github.io/blog/2016/04/memory-leaks-are-memory-...


In that scenario a memory leak isn’t any more unsafe than any other program data that’s in memory.

Every language with a garbage collector has memory safety. It's not the issue.

I mean, the threat model is that 1. Memory leaks/errors are bad 2. Programmers make those mistakes all the time 3. Using memory safe languages is cheap Therefore, 4. We should use memory safe languages more often
next

Legal | privacy