[dpdk-dev] Define statement with UB prevents compilation using UBSAN

Morten Brørup mb at smartsharesystems.com
Fri Jun 11 11:19:03 CEST 2021


> From: dev [mailto:dev-bounces at dpdk.org] On Behalf Of Stephen Hemminger
> Sent: Thursday, 10 June 2021 23.36
> 
> On Thu, 10 Jun 2021 16:51:37 -0400
> Owen Hilyard <ohilyard at iol.unh.edu> wrote:
> 
> > Working backward to the define
> > statement, AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY is defined
> as
> >
> > #define AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY (0x1 << 31)
> 
> Why not (1u << 31)?

Yes, this is better. You want the type of the defined constant to be uint32_t.

It can also be defined in hexadecimal form as (0x1u << 31), or (0b1u << 31) in binary form.

<rant>
On the CPUs/compilers supported by DPDK, the exact type of an unsigned integer is uint32_t. If the CPU/compiler used 64 bits for integers, you would need additional type casting to get it down from 64 to 32 bits.

If we were to be really pedantic, the other defined constants that fit in a signed integer, e.g. (1 << 30) should also be defined as (1u << 30) if they are supposed to be unsigned integers. However, not many people worry about signedness when writing code, especially if an unsigned value fits into a signed variable or constant.
</rant>

Thank you for your effort on this, Owen. Keep up the good work! :-)



More information about the dev mailing list