Skip to content

Commit 3e90cc9

Browse files
Ahmad-S792Ahmad Saleem
authored andcommitted
LoggingHashMap::set() and add() recurse infinitely, and the header does not compile when instantiated
https://bugs.webkit.org/show_bug.cgi?id=328442 rdar://190697626 Reviewed by Yusuke Suzuki. LoggingHashMap::set() and add() log the operation and then call themselves instead of m_map, so any use overflows the stack. fastAdd() goes through add(), so it recursed too. Nothing instantiates LoggingHashMap today, which is why this went unnoticed. Instantiating it also failed to compile for two further reasons: - Both operator= overloads were missing `return *this;`, which is an error under -Werror=return-type. - MappedPeekType named HashMap::MappedTraits, which is private. Use MappedTraitsArg::PeekType instead. The move constructor also copied other.m_map instead of moving it. Add API tests that instantiate LoggingHashMap and exercise set(), add(), fastAdd() and both assignment operators. * Source/WTF/wtf/LoggingHashMap.h: * Tools/TestWebKitAPI/CMakeLists.txt: * Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * Tools/TestWebKitAPI/Tests/WTF/LoggingHashMap.cpp: Added. (TestWebKitAPI::TEST(WTF_LoggingHashMap, SetForwardsToUnderlyingMap)): (TestWebKitAPI::TEST(WTF_LoggingHashMap, AddForwardsToUnderlyingMap)): (TestWebKitAPI::TEST(WTF_LoggingHashMap, FastAddForwardsToUnderlyingMap)): (TestWebKitAPI::TEST(WTF_LoggingHashMap, AssignmentReturnsThis)): Canonical link: https://commits.webkit.org/324768@main
1 parent 474bb3f commit 3e90cc9

4 files changed

Lines changed: 107 additions & 6 deletions

File tree

‎Source/WTF/wtf/LoggingHashMap.h‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class LoggingHashMap final {
5454
typedef typename HashMap::AddResult AddResult;
5555

5656
private:
57-
typedef typename HashMap::MappedTraits::PeekType MappedPeekType;
57+
typedef typename MappedTraitsArg::PeekType MappedPeekType;
5858

5959
public:
6060

@@ -75,7 +75,7 @@ class LoggingHashMap final {
7575
}
7676

7777
LoggingHashMap(LoggingHashMap&& other)
78-
: m_map(other.m_map)
78+
: m_map(WTF::move(other.m_map))
7979
{
8080
dataLog("auto* ", m_id, " = new HashMap(WTF::move(*", other.m_id, "));");
8181
}
@@ -84,12 +84,14 @@ class LoggingHashMap final {
8484
{
8585
dataLog("*", m_id, " = *", other.m_id, ";\n");
8686
m_map = other.m_map;
87+
return *this;
8788
}
8889

8990
LoggingHashMap& operator=(LoggingHashMap&& other)
9091
{
9192
dataLog("*", m_id, " = WTF::move(*", other.m_id, ");\n");
9293
m_map = WTF::move(other.m_map);
94+
return *this;
9395
}
9496

9597
void swap(LoggingHashMap& other)
@@ -177,7 +179,7 @@ class LoggingHashMap final {
177179
LoggingValueTraits::print(string, passedValue);
178180
string.print(");\n");
179181
dataLog(string.toUTF8CString());
180-
return set(key, std::forward<PassedType>(passedValue));
182+
return m_map.set(key, std::forward<PassedType>(passedValue));
181183
}
182184

183185
template<typename PassedType>
@@ -190,7 +192,7 @@ class LoggingHashMap final {
190192
LoggingValueTraits::print(string, passedValue);
191193
string.print(");\n");
192194
dataLog(string.toUTF8CString());
193-
return set(WTF::move(key), std::forward<PassedType>(passedValue));
195+
return m_map.set(WTF::move(key), std::forward<PassedType>(passedValue));
194196
}
195197

196198
template<typename PassedType>
@@ -203,7 +205,7 @@ class LoggingHashMap final {
203205
LoggingValueTraits::print(string, passedValue);
204206
string.print(");\n");
205207
dataLog(string.toUTF8CString());
206-
return add(key, std::forward<PassedType>(passedValue));
208+
return m_map.add(key, std::forward<PassedType>(passedValue));
207209
}
208210

209211
template<typename PassedType>
@@ -216,7 +218,7 @@ class LoggingHashMap final {
216218
LoggingValueTraits::print(string, passedValue);
217219
string.print(");\n");
218220
dataLog(string.toUTF8CString());
219-
return add(WTF::move(key), std::forward<PassedType>(passedValue));
221+
return m_map.add(WTF::move(key), std::forward<PassedType>(passedValue));
220222
}
221223

222224
template<typename PassedType>

‎Tools/TestWebKitAPI/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ set(TestWTF_SOURCES
107107
Tests/WTF/ListHashSet.cpp
108108
Tests/WTF/Lock.cpp
109109
Tests/WTF/Logger.cpp
110+
Tests/WTF/LoggingHashMap.cpp
110111
Tests/WTF/Markable.cpp
111112
Tests/WTF/MathExtras.cpp
112113
Tests/WTF/MediaTime.cpp

‎Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,7 @@
13551355
WTF/ListHashSet.cpp,
13561356
WTF/Lock.cpp,
13571357
WTF/Logger.cpp,
1358+
WTF/LoggingHashMap.cpp,
13581359
WTF/Markable.cpp,
13591360
WTF/MathExtras.cpp,
13601361
WTF/MediaTime.cpp,
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (C) 2026 Apple Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#include "config.h"
27+
#include <wtf/LoggingHashMap.h>
28+
29+
namespace TestWebKitAPI {
30+
31+
namespace {
32+
33+
constexpr char intMapTypeArguments[] = "int, int";
34+
using IntLoggingHashMap = LoggingHashMap<intMapTypeArguments, int, int>;
35+
36+
}
37+
38+
TEST(WTF_LoggingHashMap, SetForwardsToUnderlyingMap)
39+
{
40+
IntLoggingHashMap map;
41+
int key = 1;
42+
map.set(key, 10);
43+
map.set(2, 20);
44+
EXPECT_EQ(2u, map.size());
45+
46+
auto result = map.set(key, 11);
47+
EXPECT_FALSE(result.isNewEntry);
48+
EXPECT_EQ(2u, map.size());
49+
50+
unsigned sum = 0;
51+
for (auto& entry : map)
52+
sum += entry.value;
53+
EXPECT_EQ(31u, sum);
54+
}
55+
56+
TEST(WTF_LoggingHashMap, AddForwardsToUnderlyingMap)
57+
{
58+
IntLoggingHashMap map;
59+
int key = 1;
60+
EXPECT_TRUE(map.add(key, 10).isNewEntry);
61+
EXPECT_TRUE(map.add(2, 20).isNewEntry);
62+
EXPECT_FALSE(map.add(key, 99).isNewEntry);
63+
EXPECT_EQ(2u, map.size());
64+
65+
unsigned sum = 0;
66+
for (auto& entry : map)
67+
sum += entry.value;
68+
EXPECT_EQ(30u, sum);
69+
}
70+
71+
TEST(WTF_LoggingHashMap, FastAddForwardsToUnderlyingMap)
72+
{
73+
IntLoggingHashMap map;
74+
int key = 1;
75+
EXPECT_TRUE(map.fastAdd(key, 10).isNewEntry);
76+
EXPECT_TRUE(map.fastAdd(2, 20).isNewEntry);
77+
EXPECT_FALSE(map.fastAdd(key, 99).isNewEntry);
78+
EXPECT_EQ(2u, map.size());
79+
}
80+
81+
TEST(WTF_LoggingHashMap, AssignmentReturnsThis)
82+
{
83+
IntLoggingHashMap source;
84+
source.add(1, 10);
85+
86+
IntLoggingHashMap copy;
87+
IntLoggingHashMap& copyResult = (copy = source);
88+
EXPECT_EQ(&copy, &copyResult);
89+
EXPECT_EQ(1u, copy.size());
90+
91+
IntLoggingHashMap moved;
92+
IntLoggingHashMap& moveResult = (moved = WTF::move(copy));
93+
EXPECT_EQ(&moved, &moveResult);
94+
EXPECT_EQ(1u, moved.size());
95+
}
96+
97+
} // namespace TestWebKitAPI

0 commit comments

Comments
 (0)