From d2c884da860ddb5253d472d0fa1720f21bbe1b70 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Tue, 10 Nov 2020 11:51:47 -0500 Subject: [PATCH] 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 --- include/base/cef_weak_ptr.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/base/cef_weak_ptr.h b/include/base/cef_weak_ptr.h index 1ba34b9db..8f9511fcb 100644 --- a/include/base/cef_weak_ptr.h +++ b/include/base/cef_weak_ptr.h @@ -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(); }