Commit f310f66e authored by Mikhail Shatalov's avatar Mikhail Shatalov Committed by Facebook GitHub Bot

DCHECK for nullptrs in EventBaseLocal setter and factory getter

Summary: `getOrCreateFn()` is prone to undefined behavior as it returns `nullptr` reference if factory function returns a `nullptr` or empty `std::shared_ptr<T>`, or if `emplace(evb, nullptr)` is called. This diff adds `DCHECK`s to enforce non-nulls. Note that `getOrCreate()` is not affected as it uses `std::make_shared` under the hood that is guaranteed to have a new object constructed.

Differential Revision: D26246552

fbshipit-source-id: b7bc7164841b70fc0889c782438bf96a61857bbd
parent e237e691
......@@ -80,6 +80,7 @@ class EventBaseLocal : public detail::EventBaseLocalBase {
T* get(EventBase& evb) { return static_cast<T*>(getVoid(evb)); }
void emplace(EventBase& evb, T* ptr) {
DCHECK(ptr != nullptr);
std::shared_ptr<T> smartPtr(ptr);
setVoid(evb, std::move(smartPtr));
}
......@@ -110,6 +111,7 @@ class EventBaseLocal : public detail::EventBaseLocalBase {
return *static_cast<T*>(ptr);
}
std::shared_ptr<T> smartPtr(fn());
DCHECK(smartPtr.get() != nullptr);
auto& ref = *smartPtr;
setVoid(evb, std::move(smartPtr));
return ref;
......
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