Skip to content

Commit

Permalink
AK: Fix Clang 16 UBSan issue with zero-length Array
Browse files Browse the repository at this point in the history
The current implementation of `Array<T, 0>` has a zero-length C array as
its storage type. While this is accepted as a GNU extension, when
compiling with Clang 16, an UBSan error is raised every time an object
is accessed whose only field is a zero-length array.

This is likely a bug in Clang 16's implementation of UBSan, which has
been reported here: llvm/llvm-project#61775
  • Loading branch information
BertalanD authored and trflynn89 committed Mar 30, 2023
1 parent e775525 commit 0016f63
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion AK/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

namespace AK {

namespace Detail {
// This type serves as the storage of 0-sized `AK::Array`s. While zero-length `T[0]`
// is accepted as a GNU extension, it causes problems with UBSan in Clang 16.
template<typename T>
struct EmptyArrayStorage {
T& operator[](size_t) const { VERIFY_NOT_REACHED(); }
constexpr operator T*() const { return nullptr; }
};
}

template<typename T, size_t Size>
struct Array {
using ValueType = T;
Expand Down Expand Up @@ -109,7 +119,7 @@ struct Array {
return value;
}

T __data[Size];
Conditional<Size == 0, Detail::EmptyArrayStorage<T>, T[Size]> __data;
};

template<typename T, typename... Types>
Expand Down

0 comments on commit 0016f63

Please sign in to comment.