<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57032>57032</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
ubsan reports a misalignment error for operator new[] invocations to build 0-length array instance
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
RobertHenry6bev
</td>
</tr>
</table>
<pre>
Consider this C++ snippet where a deliberate misalignment of data is passed to operator new
class Thing {int a, b; ....}
size_t delta = 1;
unsigned char buf[sizeof(int)*20] ;
Thing *pThing = new (buf + delta) Thing[0];
clang++-14 builds a binary which at runtime says:
runtime error: constructor call on misaligned address 0x7fffffffe3a1 for type 'Thing', which requires 4 byte alignment
This error message is incorrect. The constructor Thing::Thing will never be called since the invocation of operator new[] asked for 0 Things.
The error message should only be generated prior to calling the constructor, not upon return from operator new.
g++-12 seems to get this right.
g++-12 does not seem to test for this.
I have not looked at the clang-14 test suite to see if this situation is explicitly tested.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx1VE1v3DgM_TX2hchAI2e-Dj5kEhTtddF7IVu0rV2N5ErypLO_vk9ymmaKXcGwZYt8fHwk3Xl9a5-9i0ZzoDSZSM-VPOOi6Mw8c6LXiQOTIs3WdBxUYrqYqKwZ3YVdIj-QVkkRXGcVI2tKnvycLX0gx6-VeKnE03qnsnoLQ_o6GTdSdTgboKhKPlNXNWfaYFWHl4_20fzL31JmgDhV80JbGH40WJDA6BC6n1Sgbhmq3Tk7-aGSR8BX8lTJJymq3Qv94frGQj7NbzvAgzS-HIFDWYkSFxCrLaAzzjvMekdKOCrKPWwfQcFYHaFaZ5wKN4ho-olUorC4ZC5MUd1i1bw5__rIIfiAr9SjIiksfVawV9aSd--iI0uldWAoKH4chnVxo7Y0wDrdZgbpw0pVHrKsa_DA3xcDNwK7G4r4XsGPaXzNHVBo0AUR1Mi5rsb1PgTu0ybrxXf01kDIpHlaBXw14Ov4in7quLAH4wgIRn8Bzl19r5JBRuicuz7ZnXN9VPwHDjkXsYLHzT1D_oNgnPxiNSSytxxxZFe6VBPNwWRJfGGRuaV78lkc5xMtM9gETktwNAR_uaN1F_29xJIi8yVm8BFDUiYnmHFK_2euPaTPwbJfdksc01oy-N55faFJXbkYW--zGiqt1HOT5fYqvnExKCOQgEhmWDlEk5ZV3VzIH7M1vUkQJnuwfgtTc7vd74XY75uTqHXb6FNzUnWCJbdLF1VWY_Yh5Q6-G_ZV-cz6Pyr3u7RFlzIDJB4suzGh-UNQN9jEpNAL9RJsO6U0lymQn3CNJk1Lt-n9BS_WXn89Hubg_0bz4dXEuHDEZncQjaynVvUDa9mcet4KvePdIB5ZH4ftvtmJQSpZW9WxjW1mKGWe6wKBPQjXppVCSnEUp-2hacRx0xxP_SCOp92-OQCDq0fBF2XsJvPY-DDWoS2UumWMOLQmpvj7EH-1PKDcroLUakmTD-1fHv_N9JlduO07vtaFQltS-Al9xMql">