<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/72032>72032</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            __builtin_object_size(P->M, 1) where M ends with a flex-array behaves like sizeof()
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            isanbard
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kees
      </td>
    </tr>
</table>

<pre>
    Using `__builtin_object_size` (and `__builtin_dynamic_object_size`) on a composite structure's member that has a flexible array loses the sense of how large it is.

For example, on a struct that has a flexible array, `__bdos` correctly says it doesn't know the size (bounded here by `alloc_size`):

```
 expect(__builtin_object_size(wrap->msg.nlmsg_content, 1), 4076);
 expect(__builtin_object_size(wrap, 0), 4096);
 expect(__builtin_object_size(wrap, 1), 4096);
```

But if it is part of a wrapper, it start behaving like `sizeof()`:

```
 expect(__builtin_object_size(wrap->msg.nlmsg_content, 1), 4076);
 expect(__builtin_object_size(&wrap->msg, 0), 4092);
    /* But suddenly gets it wrong? */
    expect(__builtin_object_size(&wrap->msg, 1), 4092);
```

https://godbolt.org/z/YrGsh8Ybs

This was recently fixed in GCC:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101832

PoC:
[composite.c.txt](https://github.com/llvm/llvm-project/files/13326039/composite.c.txt)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVLuO2zoQ_Rq6GawgjWw9ChX7uEoVIMW9RSqDosYSszRpcKjI3q-_oLTZOEa2yFYxDBkWh-ecOfOQzHqwRI3YPQhEzdJ20vcCUeyeNnIKo_PNMxFvOtdfmv9Y2wFEke733aRN0Hbvum-kwp71C4kiBYGVtP2vIf3FyqNWN6ECa3AWJCh3PDnWgYCDn1SYPAksGY507MhDGGWAUTJIOBg6684QSO_lBYxjYggjAZNlAneA0c1gpB8IdADNiUifRHq_Plvngc7yeDIk8HHlXhnf54iBay6945ifct6TCuYCLC8cWXpHbAWWAZ6tm1c5-oWiE52bbE89jOQJuktEksY4dWWByO-vNcaX63f5C3Q-kQoCq98bjtXs5elO5P8ceUisOfKwV84GsiEqzyIBPsI2LYuF6-FPYOPN9A2h_iBC9g7CTabr82EKoA9r7eAkfYg1lRCxTuQjjA7AIR50NMrvsRuNfqbobOR1B4FVZCnSv9RYgcUV8o3F-AsWAAhsBd5DtIWnvidrLjBQWPpu9s4OIm9B4H2Me7v0AQ3ZOxp-W6QxhBNHe6O4dnB950xInB8Eti8C26_-E4_V146vL_07aoZZMnhSZOP8HPSZetAWPj0-vhXrBlupZLDTK3Y3DS_aGCmw5dHN-24aEjVokbe6F_lTlmZVjtecX9xPYLF7eNsziUrCOYjdk8DqhlCHceoS5Y4CW2O-__i5O3n3bTG1PWhDLLDN8hyLNK8FtrfAWK-cm77J-zqv5YaarKjrErMqrzdjowrsClTqQKkqq21ZdWkhy22_K3c77MvDRjeYYp7FT7ErszrJJKl6lx22ebqtqqwQ25SOUpskqov-bDTzRE2JaY4bIzsy_LrTLc2wHL4udd8sGXXTwGKbGs2Bf6IEHQw17zXOl9gyn380DMzLXvsMZHuGWYfxdX3eret5mVDidUCvp3MzedP8sfFLDtH5Jcf_AwAA__8dNxKN">