OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Scopers help you manage ownership of a pointer, helping you easily manage a | 5 // Scopers help you manage ownership of a pointer, helping you easily manage a |
6 // pointer within a scope, and automatically destroying the pointer at the end | 6 // pointer within a scope, and automatically destroying the pointer at the end |
7 // of a scope. There are two main classes you will use, which correspond to the | 7 // of a scope. There are two main classes you will use, which correspond to the |
8 // operators new/delete and new[]/delete[]. | 8 // operators new/delete and new[]/delete[]. |
9 // | 9 // |
10 // Example usage (scoped_ptr<T>): | 10 // Example usage (scoped_ptr<T>): |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 // for move-only deleters. | 213 // for move-only deleters. |
214 reset(other->release()); | 214 reset(other->release()); |
215 get_deleter() = other->get_deleter(); | 215 get_deleter() = other->get_deleter(); |
216 } | 216 } |
217 | 217 |
218 ~scoped_ptr_impl() { | 218 ~scoped_ptr_impl() { |
219 if (data_.ptr != nullptr) { | 219 if (data_.ptr != nullptr) { |
220 // Not using get_deleter() saves one function call in non-optimized | 220 // Not using get_deleter() saves one function call in non-optimized |
221 // builds. | 221 // builds. |
222 static_cast<D&>(data_)(data_.ptr); | 222 static_cast<D&>(data_)(data_.ptr); |
223 // Even though |this| should no longer be accessed after destruction, | |
224 // there may be use-after-free bugs. Setting |data_.ptr| to null should | |
225 // cause many attempts to dereference |this| to segfault closer to the | |
226 // source of the use-after-free. Of course, this may not catch issues if | |
227 // the memory is immediately re-allocated and altered. | |
228 data_.ptr = nullptr; | |
danakj
2015/09/21 21:40:52
In libc++ they null the member then call the delet
danakj
2015/09/21 21:43:08
I guess that is because they reset() in the destru
Anand Mistry (off Chromium)
2015/09/22 01:13:53
Since we're in the territory of undefined behaviou
tapted
2015/09/22 02:53:09
(drive-by): the null-before-delete strategy is to
Anand Mistry (off Chromium)
2015/09/22 03:14:15
This is the defined behaviour for unique_ptr::rese
| |
223 } | 229 } |
224 } | 230 } |
225 | 231 |
226 void reset(T* p) { | 232 void reset(T* p) { |
227 // This is a self-reset, which is no longer allowed for default deleters: | 233 // This is a self-reset, which is no longer allowed for default deleters: |
228 // https://crbug.com/162971 | 234 // https://crbug.com/162971 |
229 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr); | 235 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr); |
230 | 236 |
231 // Note that running data_.ptr = p can lead to undefined behavior if | 237 // Note that running data_.ptr = p can lead to undefined behavior if |
232 // get_deleter()(get()) deletes this. In order to prevent this, reset() | 238 // get_deleter()(get()) deletes this. In order to prevent this, reset() |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
583 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 589 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
584 return scoped_ptr<T>(ptr); | 590 return scoped_ptr<T>(ptr); |
585 } | 591 } |
586 | 592 |
587 template <typename T> | 593 template <typename T> |
588 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) { | 594 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) { |
589 return out << p.get(); | 595 return out << p.get(); |
590 } | 596 } |
591 | 597 |
592 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 598 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |