On Thu Jan 18 12:00:56 2024 +0000, Yuxuan Shui wrote:
IIUC this is guaranteed by C standard to never fail? Do you have an example where alignment breaks things?
Well it fails in this case actually on 64-bit, because GUID alignment is 4 whereas the struct alignment is 8. The GUID array is aligned on 4 bytes and fitted with the ULONG preceding it but some padding is then added after it in the struct. Using SIZE_T or UINT64 instead for `num_of_guids` will fix it.
The potential undefined behavior then is that if you do `calloc(1, offsetof(struct bla, array[count]))`, which you don't here but could and perhaps should do, you may end up with a sub-allocated struct when count is 0. Any attempt to access one of the struct member in that case is UB.