Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Use After Free #48

Merged
merged 1 commit into from
Jul 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/string_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::borrow::Borrow;
use std::cell::{Cell,RefCell};
use std::cmp::max;
use std::collections::LinkedList;
use std::collections::hash_map::HashMap;
use std::collections::hash_set::HashSet;
use std::default::Default;
use std::ops::Deref;
use std::slice;
Expand Down Expand Up @@ -131,7 +131,7 @@ pub struct StringPool {
start: Cell<*mut u8>,
end: Cell<*const u8>,
chunks: RefCell<LinkedList<Chunk>>,
index: RefCell<HashMap<InternedString, InternedString>>,
index: RefCell<HashSet<InternedString>>,
}

static CAPACITY: usize = 10240;
Expand All @@ -149,10 +149,13 @@ impl StringPool {
pub fn intern<'s>(&'s self, s: &str) -> &'s str {
if s == "" { return ""; }

let search_string = InternedString::from_str(s);

let mut index = self.index.borrow_mut();
let interned_str = *index.entry(search_string).or_insert_with(|| self.do_intern(s));
if let Some(interned) = index.get(s) {
return unsafe { mem::transmute(interned as &str) };
}

let interned_str = self.do_intern(s);
index.insert(interned_str);

// The lifetime is really matched to us
unsafe { mem::transmute(interned_str) }
Expand Down