Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2013 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/6068516 to your computer and use it in GitHub Desktop.
Save anonymous/6068516 to your computer and use it in GitHub Desktop.
Minimal example for "Segmentation fault: 11" occurrence when passing around closures.
use std::vec;
fn encrypt_decrypt_consistent_key() -> (@fn(&[u8]) -> ~[u8], @fn(&[u8]) -> ~[u8]) {
let key = "random key".as_bytes().to_owned();
(|bytes: &[u8]| {
vec::append(bytes.to_owned(), key)
}, |bytes: &[u8]| {
// segfault: 11
vec::append(bytes.to_owned(), key)
})
}
fn main() {
let (encrypt, decrypt) = encrypt_decrypt_consistent_key();
let plaintext_bytes = "plaintext".as_bytes();
let encrypted_bytes = encrypt(plaintext_bytes);
// segfault: 11
let decrypted_bytes = decrypt(encrypted_bytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment