Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: Source/wtf/Optional.h

Issue 1159623012: Intensify security checks for WTF::Optional. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: store a pointer internally so failure mode is null dereference Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 #ifndef Optional_h 5 #ifndef Optional_h
6 #define Optional_h 6 #define Optional_h
7 7
8 #include "wtf/Alignment.h" 8 #include "wtf/Alignment.h"
9 #include "wtf/Assertions.h" 9 #include "wtf/Assertions.h"
10 #include "wtf/Noncopyable.h" 10 #include "wtf/Noncopyable.h"
(...skipping 14 matching lines...) Expand all
25 // recorder.emplace(constructor, args, here); 25 // recorder.emplace(constructor, args, here);
26 // // recorder destroyed at end of scope 26 // // recorder destroyed at end of scope
27 // 27 //
28 // Note in particular that unlike a pointer, though, dereferencing a const 28 // Note in particular that unlike a pointer, though, dereferencing a const
29 // optional yields a const reference. 29 // optional yields a const reference.
30 30
31 template <typename T> 31 template <typename T>
32 class Optional { 32 class Optional {
33 WTF_MAKE_NONCOPYABLE(Optional); 33 WTF_MAKE_NONCOPYABLE(Optional);
34 public: 34 public:
35 Optional() : m_engaged(false) { } 35 Optional() : m_ptr(nullptr) { }
36 ~Optional() 36 ~Optional()
37 { 37 {
38 if (m_engaged) 38 if (m_ptr)
39 storage()->~T(); 39 m_ptr->~T();
40 } 40 }
41 41
42 typedef bool Optional::*UnspecifiedBoolType; 42 typedef T* Optional::*UnspecifiedBoolType;
43 operator UnspecifiedBoolType() const { return m_engaged ? &Optional::m_engag ed : nullptr; } 43 operator UnspecifiedBoolType() const { return m_ptr ? &Optional::m_ptr : nul lptr; }
esprehn 2015/06/02 22:13:08 This doesn't need the branch in it, you can just r
jbroman 2015/06/02 23:40:17 This is the pattern used in OwnPtr::operator Unspe
esprehn 2015/06/02 23:57:08 Right, just return a void* or something. Having a
44 44
45 T& operator*() { ASSERT(m_engaged); return *storage(); } 45 T& operator*() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; }
46 const T& operator*() const { ASSERT(m_engaged); return *storage(); } 46 const T& operator*() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return *m_ptr; }
47 T* operator->() { ASSERT(m_engaged); return storage(); } 47 T* operator->() { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); return m_ptr; }
48 const T* operator->() const { ASSERT(m_engaged); return storage(); } 48 const T* operator->() const { ASSERT_WITH_SECURITY_IMPLICATION(m_ptr); retur n m_ptr; }
49 49
50 template <typename... Args> 50 template <typename... Args>
51 void emplace(Args&&... args) 51 void emplace(Args&&... args)
52 { 52 {
53 ASSERT(!m_engaged); 53 RELEASE_ASSERT(!m_ptr);
54 new (storage()) T(forward<Args>(args)...); 54 m_ptr = reinterpret_cast_ptr<T*>(&m_storage.buffer);
55 m_engaged = true; 55 new (m_ptr) T(forward<Args>(args)...);
56 } 56 }
57 57
58 private: 58 private:
59 T* storage() { return reinterpret_cast_ptr<T*>(&m_storage.buffer); } 59 T* m_ptr;
60 const T* storage() const { return reinterpret_cast_ptr<const T*>(&m_storage. buffer); }
61
62 bool m_engaged;
63 AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage; 60 AlignedBuffer<sizeof(T), WTF_ALIGN_OF(T)> m_storage;
64 }; 61 };
65 62
66 } // namespace WTF 63 } // namespace WTF
67 64
68 using WTF::Optional; 65 using WTF::Optional;
69 66
70 #endif // Optional_h 67 #endif // Optional_h
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698