Skip to content

Commit 0af6a2c

Browse files
committed
Address Safer C++ static analysis warnings in WTF headers
https://bugs.webkit.org/show_bug.cgi?id=327810 Reviewed by Ryosuke Niwa. Fix Safer C++ warnings reported in WTF headers when other projects include them. generate-dirty-files doesn't attribute warnings in these header copies to any project, so they don't appear in expectations files, but they make up a large share of the bot logs. Where the code was unsafe or could be written in a way the analyzer understands, fix it. Suppress CRTP casts and the weak pointer primitives the analyzer doesn't model, with a comment explaining why each is safe. Suppress analyzer false positives with a FIXME pointing to their radar: rdar://190192936, rdar://190195858 and rdar://190195764. * Source/WTF/wtf/ButterflyArray.h: Route CRTP casts through suppressed derived() helpers. * Source/WTF/wtf/CompactPointerTuple.h: (WTF::CompactPointerTuple::setType): Update the type bits without decoding the pointer. * Source/WTF/wtf/CompactRefPtrTuple.h: * Source/WTF/wtf/CompactUniquePtrTuple.h: * Source/WTF/wtf/CompletionHandler.h: * Source/WTF/wtf/DeferrableRefCounted.h: * Source/WTF/wtf/DoublyLinkedList.h: * Source/WTF/wtf/Function.h: * Source/WTF/wtf/GenericTimeMixin.h: * Source/WTF/wtf/InlineWeakPtr.h: * Source/WTF/wtf/InlineWeakRef.h: * Source/WTF/wtf/JSONValues.h: (WTF::JSONImpl::ArrayOf::castedArray): Removed. Call the ArrayBase methods directly instead of casting to Array. * Source/WTF/wtf/ListHashSet.h: (WTF::ListHashSet::remove): Look up the node by value, like removeFirst() and removeLast(). * Source/WTF/wtf/NativePromise.h: * Source/WTF/wtf/OSObjectPtr.h: * Source/WTF/wtf/RefCountedWithInlineWeakPtr.h: * Source/WTF/wtf/RefPtr.h: * Source/WTF/wtf/Seconds.h: * Source/WTF/wtf/SentinelLinkedList.h: * Source/WTF/wtf/StdLibExtras.h: (WTF::move): Return std::move(value); T is always an lvalue reference. (WTF::addressOfArgument): Added. Like std::addressof(), but the analyzer traces the result back to the argument (rdar://190195680). (WTF::equalSpans): Mark as LEGACY_NODELETE. * Source/WTF/wtf/ThreadSafeRefCountedWithSuppressingSaferCPPChecking.h: * Source/WTF/wtf/TrailingArray.h: * Source/WTF/wtf/UnionFind.h: * Source/WTF/wtf/UniqueRef.h: * Source/WTF/wtf/UniquelyOwnedPtr.h: * Source/WTF/wtf/Variant.h: Use std::forward and std::invoke_result_t, which the analyzer understands, instead of mpark's equivalents. * Source/WTF/wtf/Vector.h: (WTF::Vector::appendSlowCase): (WTF::Vector::insert): Use addressOfArgument(). * Source/WTF/wtf/WeakPtrImpl.h: * Source/WTF/wtf/cf/TypeCastsCF.h: (WTF::bridgeCFCast): Mark as CLANG_POINTER_CONVERSION. * Source/WTF/wtf/unicode/UTF8Conversion.h: Mark both equal() overloads as LEGACY_NODELETE. * Source/WebCore/platform/graphics/mac/controls/ControlFactoryMac.mm: Include the *Part.h headers for the parts passed to makeUnique<>(). Canonical link: https://commits.webkit.org/323956@main
1 parent ad96ea9 commit 0af6a2c

32 files changed

Lines changed: 137 additions & 94 deletions

‎Source/WTF/wtf/ButterflyArray.h‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ class ButterflyArray {
101101
void operator delete(ButterflyArray* base, std::destroying_delete_t)
102102
{
103103
unsigned leadingSize = base->m_leadingSize;
104-
std::destroy_at(static_cast<Derived*>(base));
105-
fastFree(std::bit_cast<uint8_t*>(static_cast<Derived*>(base)) - memoryOffsetForDerived(leadingSize));
104+
std::destroy_at(base->derived());
105+
fastFree(std::bit_cast<uint8_t*>(base->derived()) - memoryOffsetForDerived(leadingSize));
106106
}
107107

108108
~ButterflyArray()
@@ -116,26 +116,31 @@ class ButterflyArray {
116116
protected:
117117
LeadingType* leadingData()
118118
{
119-
return std::bit_cast<LeadingType*>(static_cast<Derived*>(this)) - m_leadingSize;
119+
return std::bit_cast<LeadingType*>(derived()) - m_leadingSize;
120120
}
121121

122122
const LeadingType leadingData() const
123123
{
124-
return std::bit_cast<const LeadingType*>(static_cast<const Derived*>(this)) - m_leadingSize;
124+
return std::bit_cast<const LeadingType*>(derived()) - m_leadingSize;
125125
}
126126

127127
TrailingType* trailingData()
128128
{
129-
return std::bit_cast<TrailingType*>(std::bit_cast<uint8_t*>(static_cast<Derived*>(this)) + offsetOfTrailingData());
129+
return std::bit_cast<TrailingType*>(std::bit_cast<uint8_t*>(derived()) + offsetOfTrailingData());
130130
}
131131

132132
const TrailingType* trailingData() const
133133
{
134-
return std::bit_cast<const TrailingType*>(std::bit_cast<const uint8_t*>(static_cast<const Derived*>(this)) + offsetOfTrailingData());
134+
return std::bit_cast<const TrailingType*>(std::bit_cast<const uint8_t*>(derived()) + offsetOfTrailingData());
135135
}
136136

137137
unsigned m_leadingSize { 0 };
138138
unsigned m_trailingSize { 0 };
139+
140+
private:
141+
// Derived derives from ButterflyArray (CRTP), so this is always a Derived.
142+
Derived* derived() { SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<Derived*>(this); }
143+
const Derived* derived() const { SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<const Derived*>(this); }
139144
};
140145

141146
} // namespace WTF

‎Source/WTF/wtf/CompactPointerTuple.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class CompactPointerTuple final {
100100
constexpr Type type() const { return decodeType(m_data); }
101101
void setType(Type type)
102102
{
103-
m_data = encode(pointer(), type);
103+
m_data = (m_data & pointerMask) | encodeType(type);
104104
ASSERT(this->type() == type);
105105
}
106106

‎Source/WTF/wtf/CompactRefPtrTuple.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class CompactRefPtrTuple final {
5454

5555
CompactRefPtrTuple(const CompactRefPtrTuple& other)
5656
{
57-
setPointer(other.pointer());
57+
// other.pointer() is kept alive by the reference that other holds.
58+
SUPPRESS_UNCOUNTED_ARG setPointer(other.pointer());
5859
setType(other.type());
5960
}
6061

‎Source/WTF/wtf/CompactUniquePtrTuple.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ class CompactUniquePtrTuple final {
113113

114114
void deletePointer()
115115
{
116-
if (T* pointer = m_data.pointer())
116+
// This tuple owns the pointer and is deleting it.
117+
if (SUPPRESS_UNCHECKED_LOCAL T* pointer = m_data.pointer())
117118
Deleter()(pointer);
118119
}
119120

‎Source/WTF/wtf/CompletionHandler.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ IGNORE_CLANG_WARNINGS_END
107107
{
108108
assertIsCurrent(m_callThread);
109109
ASSERT_WITH_MESSAGE(m_function, "Completion handler should not be called more than once");
110-
return std::exchange(m_function, nullptr)(std::forward<In>(in)...);
110+
// FIXME: Remove this suppression once rdar://190195858 is fixed.
111+
SUPPRESS_FORWARD_DECL_ARG return std::exchange(m_function, nullptr)(std::forward<In>(in)...);
111112
}
112113

113114
private:

‎Source/WTF/wtf/DeferrableRefCounted.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ class DeferrableRefCounted : public DeferrableRefCountedBase {
9999
public:
100100
void deref() const
101101
{
102+
// T derives from DeferrableRefCounted<T> (CRTP), so this is always a T.
102103
if (derefBase())
103-
delete static_cast<const T*>(this);
104+
SUPPRESS_MEMORY_UNSAFE_CAST delete static_cast<const T*>(this);
104105
}
105106

106107
bool setIsDeferred(bool value)
107108
{
108109
if (!setIsDeferredBase(value))
109110
return false;
110-
delete static_cast<T*>(this);
111+
// T derives from DeferrableRefCounted<T> (CRTP), so this is always a T.
112+
SUPPRESS_MEMORY_UNSAFE_CAST delete static_cast<T*>(this);
111113
return true;
112114
}
113115

‎Source/WTF/wtf/DoublyLinkedList.h‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,26 @@ template<typename T> inline DoublyLinkedListNode<T>::DoublyLinkedListNode()
5050

5151
template<typename T> inline void DoublyLinkedListNode<T>::setPrev(T* prev)
5252
{
53-
static_cast<T*>(this)->m_prev = prev;
53+
// T derives from DoublyLinkedListNode<T> (CRTP), so this is always a T.
54+
SUPPRESS_MEMORY_UNSAFE_CAST static_cast<T*>(this)->m_prev = prev;
5455
}
5556

5657
template<typename T> inline void DoublyLinkedListNode<T>::setNext(T* next)
5758
{
58-
static_cast<T*>(this)->m_next = next;
59+
// T derives from DoublyLinkedListNode<T> (CRTP), so this is always a T.
60+
SUPPRESS_MEMORY_UNSAFE_CAST static_cast<T*>(this)->m_next = next;
5961
}
6062

6163
template<typename T> inline T* DoublyLinkedListNode<T>::prev() const
6264
{
63-
return static_cast<const T*>(this)->m_prev;
65+
// T derives from DoublyLinkedListNode<T> (CRTP), so this is always a T.
66+
SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<const T*>(this)->m_prev;
6467
}
6568

6669
template<typename T> inline T* DoublyLinkedListNode<T>::next() const
6770
{
68-
return static_cast<const T*>(this)->m_next;
71+
// T derives from DoublyLinkedListNode<T> (CRTP), so this is always a T.
72+
SUPPRESS_MEMORY_UNSAFE_CAST return static_cast<const T*>(this)->m_next;
6973
}
7074

7175
template<typename T>

‎Source/WTF/wtf/Function.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class CallableWrapper : public CallableWrapperBase<Out, In...> {
5656
: m_callable(WTF::move(callable)) { }
5757
CallableWrapper(const CallableWrapper&) = delete;
5858
CallableWrapper& operator=(const CallableWrapper&) = delete;
59-
Out call(In... in) final { return m_callable(std::forward<In>(in)...); }
59+
// FIXME: Remove this suppression once rdar://190195858 is fixed.
60+
Out call(In... in) final { SUPPRESS_FORWARD_DECL_ARG return m_callable(std::forward<In>(in)...); }
6061
private:
6162
CallableType m_callable;
6263
};
@@ -100,7 +101,8 @@ class Function<Out(In...)> {
100101
Out operator()(In... in) const
101102
{
102103
ASSERT(m_callableWrapper);
103-
return m_callableWrapper->call(std::forward<In>(in)...);
104+
// FIXME: Remove this suppression once rdar://190195858 is fixed.
105+
SUPPRESS_FORWARD_DECL_ARG return m_callableWrapper->call(std::forward<In>(in)...);
104106
}
105107

106108
explicit operator bool() const { return !!m_callableWrapper; }

‎Source/WTF/wtf/GenericTimeMixin.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ class GenericTimeMixin {
109109
requires (std::is_same_v<TargetTime, DerivedTime>)
110110
inline DerivedTime approximate() const
111111
{
112-
return *static_cast<const DerivedTime*>(this);
112+
// DerivedTime derives from GenericTimeMixin<DerivedTime> (CRTP), so this is always a DerivedTime.
113+
SUPPRESS_MEMORY_UNSAFE_CAST return *static_cast<const DerivedTime*>(this);
113114
}
114115

115116
template<typename TargetTime>

‎Source/WTF/wtf/InlineWeakPtr.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class InlineWeakPtr {
4343
InlineWeakPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
4444
InlineWeakPtr(HashTableEmptyValueType) : m_ptr(nullptr) { }
4545

46-
ALWAYS_INLINE ~InlineWeakPtr() { weakDerefIfNotNull(m_ptr); }
46+
// InlineWeakPtr is itself the weak reference that keeps m_ptr's storage alive.
47+
ALWAYS_INLINE ~InlineWeakPtr() { SUPPRESS_UNCOUNTED_ARG SUPPRESS_UNCHECKED_ARG weakDerefIfNotNull(m_ptr); }
4748

4849
bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
4950
bool isHashTableEmptyValue() const { return !m_ptr; }
@@ -100,7 +101,8 @@ inline InlineWeakPtr<T>& InlineWeakPtr<T>::operator=(T* optr)
100101
template<typename T>
101102
inline InlineWeakPtr<T>& InlineWeakPtr<T>::operator=(std::nullptr_t)
102103
{
103-
weakDerefIfNotNull(std::exchange(m_ptr, nullptr));
104+
// InlineWeakPtr is itself the weak reference that keeps m_ptr's storage alive.
105+
SUPPRESS_UNCOUNTED_ARG SUPPRESS_UNCHECKED_ARG weakDerefIfNotNull(std::exchange(m_ptr, nullptr));
104106
return *this;
105107
}
106108

0 commit comments

Comments
 (0)