Commit bfde457b authored by Robin Cheng's avatar Robin Cheng Committed by Facebook GitHub Bot

Fix an unnecessary racy access in SingleWriterFixedHashMap on empty iterator

Summary:
When grabbing an iterator when the map is empty, size is zero but we
still create a copy of the elem_ pointer which is invalid to do when the size
is zero (since the writer could overwrite the pointer upon first insertion).
This diff checks whether the iterator would be empty and if so avoid grabbing
that pointer.

Reviewed By: yfeldblum

Differential Revision: D23520616

fbshipit-source-id: 5b3f2bc3a9b935920e7fd616e64fadafd7775a75
parent b172ccdf
......@@ -319,7 +319,9 @@ class SingleWriterFixedHashMap {
friend class SingleWriterFixedHashMap;
explicit Iterator(const SingleWriterFixedHashMap& m, size_t i = 0)
: elem_(m.elem_.get()), capacity_(m.capacity_), index_(i) {
: elem_(i == m.capacity_ ? nullptr : m.elem_.get()),
capacity_(m.capacity_),
index_(i) {
if (index_ < capacity_) {
next();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment