4 ms·
Hi. Rust core here. I like to say that of course Rust prevented Heartbleed, because we don't currently support wrapping malloc ;). That's of course a bit glib.
by steveklabnik 12y ago
Hi. Rust core here.
I like to say that of course Rust prevented Heartbleed, because we don't currently support wrapping malloc ;). That's of course a bit glib.
While it's true that Rust gives you a lot of assistance with writing safe code, given that unsafe exists, and that we, as a systems language, need to give you underlying access to the machine, Rust will allow you to do all kinds of stupid things if you vouch to the compiler that you're going to handle the safety aspect. This means that ultimately, you can do any kind of stupid thing in Rust that you can do in any other language.
An example: a common problem in C code is buffer overflows, which happens when some code thinks that an array has more elements in it than it does. Because C's arrays don't have a length as part of the type, it can be error prone to keep track of the length of the array. Rust's arrays, vectors, and slices all have a length as a part of the type, and so it's more difficult to have this data be out of sync. But Rust will let you be stupid if you want to:
fn bad_stuff(v: Vec<i32>) -> i32 {
unsafe {
*(v.get_unchecked(v.len() + 1))
}
}
fn main() {
let vec = vec![1, 2, 3];
let val = bad_stuff(vec);
println!("{}", val);
}
The `bad_stuff` function here uses `get_unchecked()`, which skips the array bounds check, to grab some memory past the length of the end of the vector. Rust will happily compile this code, your `unsafe` block tells the compiler to trust that you're using `get_unchecked()` in a safe way, even though it can't verify it.
Now, there's a decent discussion that can be had on this topic: 'usual' Rust code will of course be significantly safer than 'usual' C code, since you have the compiler's assistance. Unfortunately, security is often about the edge cases. While it's true that not wrapping malloc would have prevented Heartbleed from happening, and most C projects don't wrap malloc... it did happen. Once Rust starts getting used in the wild, we'll see Rust with security problems, I'm sure of it. That doesn't mean that Rust has no value, just that we need to be realistic about the tradeoffs that any of our technologies make. Rust is not a panacea, even if it does cure some of your aches and pains.
- jonesetc 12y ago> That's of course a bit glib. I think you mean it's a bit glibc.
- Rickasaurus 12y agoWhat about adding safer checked buffer types as part of the standard library? A fixed buffer for things of "up to this size" where it remembers how big the current thing is. Seems like it would be a pretty easy problem to fix if you're not playing fast and loose.
- steveklabnik 12y agoThat's what Vectors, slices, and arrays already do. See that v.len()?
- stream_fusion 12y agoAlmost all languages have an escape-hatch to do dangerous stuff. The key insight about Rust is the set of programming abstractions (linear types to manage resources) that enable one to get systems-like programming tasks done, without needing to fall back on very low-level coding techniques.
- Dewie 12y ago> Hi. Rust core here. I for one am ready for my daily Rust PR programming.
- latiera 12y agoOh but it DOES mean that Rust has no value, at least regarding security. Once you give them the rope, they WILL use it. Let's see if time will prove me right.
- kibwen 12y agoEvery language has a C FFI that users can use as rope to hang themselves with. `unsafe` code in Rust is just a reified FFI that actually manages to make Rust code safer because it means fewer things need to be written in C, and even unsafe Rust code is safer than C. Furthermore, the strict demarcation of `unsafe` blocks greatly alleviates the burden upon code auditors, allowing them to focus their efforts. Even if an extraordinarily high percentage of your code is contained within unsafe blocks, say 10%, that's ten times less code to audit than an equivalently-sized C codebase.
- nnethercote 12y agoYou remind me of a guy I once knew who reasoned that because type systems could not prevent all bugs, they were useless. "Half a hole is still a hole," he said smugly. "But it's a much smaller hole," I replied.
- NoOneCaresSteve 12y agoI know youre happy that you are in the rust core team (for which i have no idea why) but have you ever heard of extrinsic ethos? we know that youre rust core, you dont need to say that every time. god damn it just makes me angry, you write like no rust code, all you do is talk about it, and have to be regularly corrected because you dont actually know rust. It is annoying as fuck, youre just everywhere like a plague. Damn ive wanted to say that for a long time. As a long time user and member of the community, at least from me, its annoying. stop please.
- kibwen 12y agoWhat an incredibly useless and demeaning comment. Everyone who has used Rust has needed to be corrected at times because of the changing nature of the language over the past few years, including people on the core team. When Steve has needed to be corrected he takes it graciously, and I say this as someone who has corrected Steve before. As for "extrinsic ethos", not only does HN make it trivial to gloss over usernames but most people do not expect members of a language's core team to come forward with frank explanations of its limitations. If you have problems with Steve, this is not the place to air them.