You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pubfnintern<'s>(&'sself,s:&str) -> &'sstr{if s == ""{return"";}let search_string = InternedString::from_str(s);letmut index = self.index.borrow_mut();let interned_str = *index.entry(search_string).or_insert_with(|| self.do_intern(s));// The lifetime is really matched to usunsafe{ mem::transmute(interned_str)}}
It takes a random str slice and inserts it into the pool as the key. However there's no guarantee that the key will still be alive at a later point in time, as the key itself is not interned. So later when using the HashMap, it'll compare the keys and so it'll compare to memory that isn't allocated anymore.
Thanks for the report! However, I don't agree with your assessment so I'd like your help to understand what I'm missing...
It takes a random str slice and inserts it into the pool as the key
This isn't quite true... it does take a string slice and converts it to a InternedString:
let search_string = InternedString::from_str(s);
This is only used to look up in the HashMap. If the value is found, we return the value from the pool. If it is not found, we add it to the pool and return that value. In both cases, the return value is one of the pooled values
let interned_str = *index.entry(search_string).or_insert_with(|| self.do_intern(s));
At no point is the input string returned. In fact, there's a test to make sure that specific case doesn't happen (does_not_reuse_the_pointer_of_the_input).
It's not returned, but it's stored as the key of the HashMap through the Entry API, where it later gets compared when looking up more values. The or_insert_with closure only provides the value of the HashMap, it takes the key from the entry parameter.
Ah, that makes sense! I always wanted to switch to a HashSet, but couldn't originally because the feature to get the value stored in the HashSet wasn't present. It's a good thing that they finally added it, although I guess Rust 1.9.0 was a while ago now!
Activity
CryZe commentedon Jun 28, 2017
Looks like the String Pool is super unsafe.
It takes a random str slice and inserts it into the pool as the key. However there's no guarantee that the key will still be alive at a later point in time, as the key itself is not interned. So later when using the HashMap, it'll compare the keys and so it'll compare to memory that isn't allocated anymore.
Fix Use After Free
shepmaster commentedon Jul 1, 2017
Thanks for the report! However, I don't agree with your assessment so I'd like your help to understand what I'm missing...
This isn't quite true... it does take a string slice and converts it to a
InternedString:This is only used to look up in the
HashMap. If the value is found, we return the value from the pool. If it is not found, we add it to the pool and return that value. In both cases, the return value is one of the pooled valuesAt no point is the input string returned. In fact, there's a test to make sure that specific case doesn't happen (
does_not_reuse_the_pointer_of_the_input).CryZe commentedon Jul 1, 2017
It's not returned, but it's stored as the key of the HashMap through the Entry API, where it later gets compared when looking up more values. The or_insert_with closure only provides the value of the HashMap, it takes the key from the entry parameter.
shepmaster commentedon Jul 1, 2017
Ah, that makes sense! I always wanted to switch to a
HashSet, but couldn't originally because the feature to get the value stored in theHashSetwasn't present. It's a good thing that they finally added it, although I guess Rust 1.9.0 was a while ago now!Fix Use After Free
Fix Use After Free
Fix Use After Free