Skip to content

Commit bbdbbe5

Browse files
committed
CompactVariableMap::Handle's copy operator= leaks the previous data
https://bugs.webkit.org/show_bug.cgi?id=195398 Reviewed by Yusuke Suzuki. The copy constructor was just assigning |this| to the new value, forgetting to decrement the ref count of the thing pointed to by the |this| handle. Based on Yusuke's suggestion, this patch refactors the move constructor, move operator=, and copy operator= to use the swap() primitive and the copy constructor primitive. * parser/VariableEnvironment.cpp: (JSC::CompactVariableMap::Handle::Handle): (JSC::CompactVariableMap::Handle::swap): (JSC::CompactVariableMap::Handle::operator=): Deleted. * parser/VariableEnvironment.h: (JSC::CompactVariableMap::Handle::Handle): (JSC::CompactVariableMap::Handle::operator=): Canonical link: https://commits.webkit.org/209752@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242613 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent ffdcf4d commit bbdbbe5

3 files changed

Lines changed: 53 additions & 27 deletions

File tree

‎Source/JavaScriptCore/ChangeLog‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
2019-03-07 Saam Barati <sbarati@apple.com>
2+
3+
CompactVariableMap::Handle's copy operator= leaks the previous data
4+
https://bugs.webkit.org/show_bug.cgi?id=195398
5+
6+
Reviewed by Yusuke Suzuki.
7+
8+
The copy constructor was just assigning |this| to the new value,
9+
forgetting to decrement the ref count of the thing pointed to by
10+
the |this| handle. Based on Yusuke's suggestion, this patch refactors
11+
the move constructor, move operator=, and copy operator= to use the
12+
swap() primitive and the copy constructor primitive.
13+
14+
* parser/VariableEnvironment.cpp:
15+
(JSC::CompactVariableMap::Handle::Handle):
16+
(JSC::CompactVariableMap::Handle::swap):
17+
(JSC::CompactVariableMap::Handle::operator=): Deleted.
18+
* parser/VariableEnvironment.h:
19+
(JSC::CompactVariableMap::Handle::Handle):
20+
(JSC::CompactVariableMap::Handle::operator=):
21+
122
2019-03-07 Tadeu Zagallo <tzagallo@apple.com>
223

324
Lazily decode cached bytecode

‎Source/JavaScriptCore/parser/VariableEnvironment.cpp‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,26 +193,20 @@ CompactVariableMap::Handle::~Handle()
193193
}
194194

195195
CompactVariableMap::Handle::Handle(const CompactVariableMap::Handle& other)
196+
: m_environment(other.m_environment)
197+
, m_map(other.m_map)
196198
{
197-
*this = other;
198-
}
199-
200-
CompactVariableMap::Handle& CompactVariableMap::Handle::operator=(const Handle& other)
201-
{
202-
m_map = other.m_map;
203-
m_environment = other.m_environment;
204-
205-
if (!m_map) {
206-
ASSERT(!m_environment);
207-
// This happens if `other` were moved into a different handle.
208-
return *this;
199+
if (m_map) {
200+
auto iter = m_map->m_map.find(CompactVariableMapKey { *m_environment });
201+
RELEASE_ASSERT(iter != m_map->m_map.end());
202+
++iter->value;
209203
}
204+
}
210205

211-
auto iter = m_map->m_map.find(CompactVariableMapKey { *m_environment });
212-
RELEASE_ASSERT(iter != m_map->m_map.end());
213-
++iter->value;
214-
215-
return *this;
206+
CompactVariableMap::Handle::Handle(CompactVariableEnvironment& environment, CompactVariableMap& map)
207+
: m_environment(&environment)
208+
, m_map(&map)
209+
{
216210
}
217211

218212
} // namespace JSC

‎Source/JavaScriptCore/parser/VariableEnvironment.h‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,26 @@ class CompactVariableMap : public RefCounted<CompactVariableMap> {
214214
public:
215215
Handle() = default;
216216

217-
Handle(CompactVariableEnvironment& environment, CompactVariableMap& map)
218-
: m_environment(&environment)
219-
, m_map(&map)
220-
{ }
217+
Handle(CompactVariableEnvironment&, CompactVariableMap&);
218+
221219
Handle(Handle&& other)
222-
: m_environment(other.m_environment)
223-
, m_map(WTFMove(other.m_map))
224220
{
225-
RELEASE_ASSERT(!!m_environment == !!m_map);
226-
ASSERT(!other.m_map);
227-
other.m_environment = nullptr;
221+
swap(other);
222+
}
223+
Handle& operator=(Handle&& other)
224+
{
225+
Handle handle(WTFMove(other));
226+
swap(handle);
227+
return *this;
228228
}
229229

230230
Handle(const Handle&);
231-
Handle& operator=(const Handle&);
231+
Handle& operator=(const Handle& other)
232+
{
233+
Handle handle(other);
234+
swap(handle);
235+
return *this;
236+
}
232237

233238
~Handle();
234239

@@ -240,6 +245,12 @@ class CompactVariableMap : public RefCounted<CompactVariableMap> {
240245
}
241246

242247
private:
248+
void swap(Handle& other)
249+
{
250+
std::swap(other.m_environment, m_environment);
251+
std::swap(other.m_map, m_map);
252+
}
253+
243254
CompactVariableEnvironment* m_environment { nullptr };
244255
RefPtr<CompactVariableMap> m_map;
245256
};

0 commit comments

Comments
 (0)