Prevent UB if a WeakPtr to an already-destroyed object is dereferenced.

If a WeakPtr references an already-destroyed object, operator-> and
operator* end up simply dereferencing nullptr. However, dereferencing
nullptr is undefined behavior and can be optimized in surprising ways
by compilers. To prevent this from happening, add a defence of last
resort and CHECK that the WeakPtr is still valid.

Based on https://crrev.com/bbb64b5c69
This commit is contained in:
Marshall Greenblatt 2020-11-10 11:51:47 -05:00
parent 0b7840ab2f
commit d2c884da86
1 changed files with 2 additions and 2 deletions

View File

@ -250,11 +250,11 @@ class WeakPtr : public cef_internal::WeakPtrBase {
T* get() const { return ref_.is_valid() ? ptr_ : NULL; }
T& operator*() const {
DCHECK(get() != NULL);
CHECK(ref_.is_valid());
return *get();
}
T* operator->() const {
DCHECK(get() != NULL);
CHECK(ref_.is_valid());
return get();
}