-
Notifications
You must be signed in to change notification settings - Fork 37
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
Conversation
I'd appreciate it if you'd avoid applying styling changes at the same time as any fixes. This code is quite old and predates many of the current best practices, so I'd prefer to apply style fixes en masse in a completely separate PR. In general, coupling these completely distinct sets of changes makes it very difficult to see what is changing and why (and makes |
Okay, I'll revert those, give me a second. |
cb710d1
to
b5be12b
Compare
Alright, I reverted all the styling changes. |
src/string_pool.rs
Outdated
} | ||
|
||
index.insert(self.do_intern(s)); | ||
let interned_str: &str = index.get(s).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, there'd be some kind of entry
-esque API so we reduce it down to a single hash calculation. I think that would need something akin to rust-lang/rfcs#1769 though.
In the meantime, I think it's OK to have extra hashes for the case where we have to insert anyway. However, can we reduce it from 3 to 2 with something like this?
let foo = self.do_intern(s);
index.insert(foo);
unsafe { mem::transmute(foo) }
That is, it's safe to copy the interned string as the pointer points into the pool and the length is just a number.
The String Pool now uses just a HashSet, that stores the actual Interned Strings. The old code also stored the str slices that we where looking for as keys, but there were never interned properly, so they were super likely to get freed at some point and cause a Use after Free. Fixes shepmaster#47
Alright, I got rid of the additional lookup. |
Is there anything left to do? |
Sorry, I was on a plane the last time we were talking and forgot about this 😊 I wanted to record some performance numbers: Baseline
3-lookup (original PR)
2-lookup (current state of the PR)
|
Thank you very much for fixing this, and I'm sorry for any inconvenience we caused! |
Released version 0.2.3 |
Don't worry about it, it never caused any trouble in practice. Thanks for releasing 0.2.3 😄 |
The String Pool now uses just a HashSet, that stores the actual Interned Strings. The old code also stored the str slices that we where looking for as keys, but they were never interned properly, so they were super likely to get freed at some point and cause a Use after Free.
Fixes #47