<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61775>61775</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[UBSan] Regression: Bogus "insufficient space for object" error with struct containing 0-length array
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:codegen,
compiler-rt:ubsan,
regression
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
BertalanD
</td>
</tr>
</table>
<pre>
*Reproducer on Godbolt: https://godbolt.org/z/sfjrvnM7f*
Consider the following code:
```c++
#include <span>
template<typename T, size_t N>
struct my_array {
T m_data[N];
operator std::span<T>() { return { m_data, N }; }
};
struct base {
std::span<int> m_values;
};
template<typename... Args>
struct storage : base {
storage(Args const&... args)
: m_data({ args... })
{
m_values = m_data;
}
my_array<int, sizeof...(Args)> m_data;
};
int main() {
storage<> bug_struct;
}
```
When compiling this with Clang 16 with the UBSan object size checker enabled:
```console
clang++-16 -std=c++20 -fsanitize=object-size
```
The following error is produced at runtime:
```
/app/example.cpp:24:20: runtime error: member call on address 0x7ffe95aab8d0 with insufficient space for an object of type 'my_array<int, 0>'
0x7ffe95aab8d0: note: pointer points here
00 00 00 00 d0 d9 c3 54 48 56 00 00 83 80 10 06 0d 7f 00 00 20 e6 6e 06 0d 7f 00 00 c8 b9 aa 95
^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /app/example.cpp:24:20 in
```
This error is clearly bogus, as an empty type requires a single byte of storage space, and the pointer does not point to the end of the address space. The issue seems to be caused by the `T[0]` member in the `my_array` struct; adding some template parameters to `bug_struct`, or using `std::array` proper instead of the custom type makes the issue go away.
This seems to be a regression: it used to run fine with Clang 15, and it still shows no problems with the current version of GCC.
I'm aware that I shouldn't expect non-standard extensions to always work nicely, but I don't see how the code in the example would have any genuine bug. Looking at the IR, the object size check seems to be folded to an unconditional call to `__ubsan_handle_type_mismatch_v1`, so this might be a codegen/optimizer issue.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