]> www.infradead.org Git - users/sagi/libnvme.git/commit
test: Cast values to u32 if shift overflows int
authorCaleb Sander <csander@purestorage.com>
Thu, 2 Nov 2023 13:29:56 +0000 (14:29 +0100)
committerDaniel Wagner <wagi@monom.org>
Thu, 2 Nov 2023 13:31:26 +0000 (14:31 +0100)
commitd5962d8fa41ca0ad412d86bf0aed2fbd01be93fd
tree031b6b0d14ed414627abb51f8be670855d32cdee
parent3ca94c0667c05c7d5d163e0e6d4ed27f90d89ece
test: Cast values to u32 if shift overflows int

Bit shifts that overflow the resulting type are undefined behavior in C.
C arithmetic promotes to ints all smaller integer types.
There are several places where a 32-bit unsigned value
is constructed by shifting a u8 or u16 to the most significant bits.
Since this overflows a signed 32-bit integer,
explicitly cast to u32 to avoid the UB.
Technically, an int is allowed to only be 16 bits,
so any shift that could set bit 15 or higher is UB.
But platforms where int is s16 are not very common,
so it's likely not worth the effort to fix the code.

Signed-off-by: Caleb Sander <csander@purestorage.com>
test/ioctl/features.c
test/mi.c