Wasn't the heartbleed issue that you could trick it into reading past the memory it had allocated? That's different to explicitly reusing memory you've allocated without clearing it in between.<p>The original claim was that rust would prevent the class of errors that caused Heartbleed. No one claimed rust would prevent you from writing a program with a different bug that just happens to exhibit similar behavior.<p>Buffer overruns are tricker to spot than explicitly reusing a buffer.<p>[Edit]
An example of an actual buffer overrun, with no changes to pingback.<p>C:<p><pre><code> $:/tmp # cat bleed.c
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
void
pingback(char *path, char *outpath, unsigned char *buffer)
{
int fd;
if ((fd = open(path, O_RDONLY)) == -1)
assert(!"open");
if (read(fd, buffer, 256) < 1)
assert(!"read");
close(fd);
size_t len = buffer[0];
if ((fd = creat(outpath, 0644)) == -1)
assert(!"creat");
if (write(fd, buffer, len) != len)
assert(!"write");
close(fd);
}
int
main(int argc, char **argv)
{
unsigned char buffer2[10];
unsigned char buffer1[10];
pingback("yourping", "yourecho", buffer1);
pingback("myping", "myecho", buffer2);
}
$:/tmp # gcc bleed.c && ./a.out && cat yourecho myecho
#i have many secrets. this is one.
#i know your
one.
Æ+x-core:/tmp #
</code></pre>
Rust:<p><pre><code> C:\Users\ajanuary\Desktop>cat hearbleed.rs
use std::old_io::File;
fn pingback(path : Path, outpath : Path, buffer : &mut[u8]) {
let mut fd = File::open(&path);
match fd.read(buffer) {
Err(what) => panic!("say {}", what),
Ok(x) => if x < 1 { return; }
}
let len = buffer[0] as usize;
let mut outfd = File::create(&outpath);
match outfd.write_all(&buffer[0 .. len]) {
Err(what) => panic!("say {}", what),
Ok(_) => ()
}
}
fn main() {
let buffer2 = &mut[0u8; 10];
let buffer1 = &mut[0u8; 10];
pingback(Path::new("yourping"), Path::new("yourecho"), buffer1);
pingback(Path::new("myping"), Path::new("myecho"), buffer2);
}
C:\Users\ajanuary\Desktop>hearbleed.exe
thread '<main>' panicked at 'assertion failed: index.end <= self.len()', C:\bot\slave\nightly-dist-rustc-win-64\build\src\libcore\slice.rs:524</code></pre>