Skip to content

Instantly share code, notes, and snippets.

@raphlinus
Created March 13, 2019 23:36
Show Gist options
  • Save raphlinus/2cddf813aa7bdca226a3ec893d5b797e to your computer and use it in GitHub Desktop.
Save raphlinus/2cddf813aa7bdca226a3ec893d5b797e to your computer and use it in GitHub Desktop.
Example of unsoundness in harfbuzz crate
use harfbuzz::Blob;
fn create_blob() -> Blob {
let vec = vec![1; 256];
Blob::new_read_only(&vec)
// BAD: vec is dropped here, the blob still holds a reference
}
fn blob_sum(blob: &Blob) -> u32 {
blob.iter().map(|byte| *byte as u32).sum()
}
fn main() {
let vec = vec![1; 256];
let blob_ok = Blob::new_read_only(&vec);
let blob_bad = create_blob();
println!("sum of bytes should be {}", blob_sum(&blob_ok));
println!("sum of bytes (bad) is {}", blob_sum(&blob_bad));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment