I was looking at a port of libusb today for android and I noticed this line of code:
struct usbi_pollfd *ipollfd = malloc(sizeof(*ipollfd));
It seems that ipollfd
is being allocated based on the size of itself which has not been completely allocated yet. My first thought would be that the behavior of this is undefined. Is that the case?
It's fine and well-defined behaviour.
sizeof
gets evaluated at compile-time, and unless the operand is VLA, the operand is not evaluated. (So, no invalid-pointer dererefence, as it might look like)
To put it in other words, sizeof
only needs to know the type of the operand (which is already defined).
Quoting C11
, chapter §6.5.3.4
[....] If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With