thom311 / libnl

Netlink Library Suite
GNU Lesser General Public License v2.1
419 stars 311 forks source link

Fix ubsan complaint about incorrect left-shift in generate_local_port() #379

Closed ievenbach closed 4 months ago

ievenbach commented 4 months ago

The value of "n" used to calculate get the unique port within application is 10 bit. This change applies corresponding mask to n, prior to left-shifting it.

thom311 commented 4 months ago

I am not sure that this is right. n & 0x3ff is a positive signed integer of at most 0x3ff. If you shift that signed value by 22 bits to the left, then the value can become negative, which is again undefined behavior. I think, this would require at least n & 0x3ffu.

I think the problem here is that shifting a u16 value by 22 is undefined behavior. If so, a better fix is ((uint32_t) n) << 22).

Does that also avoid the ubsan issue?

ievenbach commented 4 months ago

OK. You are right. I simply changed data type of n, and it worked. Is there a good reason for n to still be uint16_t?

thom311 commented 4 months ago

merged. Thank you!!