FastLED 3.10.8
Loading...
Searching...
No Matches

◆ operator[]()

template<typename Key, typename Value, typename Less = fl::less<Key>>
Value & fl::flat_map< Key, Value, Less >::operator[] ( const Key & key)
inline

Definition at line 105 of file flat_map.h.

105 {
106 auto it = lower_bound(key);
107 if (it != end() && !mLess(key, it->first) && !mLess(it->first, key)) {
108 return it->second;
109 }
110 // Key not found, insert it with default value.
111 bool success = mData.insert(it, value_type(key, Value()));
112 if (!success) {
113 // FastLED#3588: under memory pressure the backing vector
114 // could not grow, so the key was NOT inserted. The old code
115 // then did `find(key)` (which returns end(), the key being
116 // absent) and returned `end()->second` — a reference ONE PAST
117 // the buffer. The caller's assignment through it wrote a value
118 // into the neighboring heap allocation, corrupting it (e.g. a
119 // shared_ptr control block, detonating later as a wild-pointer
120 // refcount decrement). Degrade gracefully instead: hand back a
121 // discardable sink so the caller's write is dropped rather than
122 // corrupting the heap. This matches the library's no-throw OOM
123 // policy (a failed insert simply does not take effect). The
124 // sink is a process-lifetime Singleton (shared, never
125 // destroyed) so the reference stays valid across DLL
126 // boundaries and does not add per-map storage.
128 sink = Value();
129 return sink;
130 }
131 // Find the newly inserted element
132 it = find(key);
133 return it->second;
134 }
static T & instance() FL_NO_EXCEPT
Definition singleton.h:81
iterator find(const Key &key) FL_NO_EXCEPT
Definition flat_map.h:156
fl::pair< Key, Value > value_type
Definition flat_map.h:37
iterator lower_bound(const Key &key) FL_NO_EXCEPT
Definition flat_map.h:208
iterator end() FL_NO_EXCEPT
Definition flat_map.h:86
vector_type mData
Definition flat_map.h:53