// Copyright 2022 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef BASE_MEMORY_WEAK_AUTO_RESET_H_ #define BASE_MEMORY_WEAK_AUTO_RESET_H_ #include "base/memory/weak_ptr.h" namespace base { // Sets a field of an object to a specified value, then returns it to its // original value when the WeakAutoReset instance goes out of scope. Because a // weak pointer is used, if the target object is destroyed, no attempt is made // to restore the original value and no UAF occurs. // // Note that as of C++17 we can use CTAD to infer template parameters from // constructor args; it is valid to write: // WeakAutoReset war(myobj->GetWeakPtr(), &MyClass::member_, new_value); // without specifying explicit types in the classname. template <class T, class U> class WeakAutoReset { … }; } // namespace base #endif // BASE_MEMORY_WEAK_AUTO_RESET_H_