]> www.infradead.org Git - users/sagi/libnvme.git/commit
mi: Cast values to u32 if shift overflows int
authorCaleb Sander <csander@purestorage.com>
Thu, 2 Nov 2023 13:29:46 +0000 (14:29 +0100)
committerDaniel Wagner <wagi@monom.org>
Thu, 2 Nov 2023 13:31:26 +0000 (14:31 +0100)
commit3ca94c0667c05c7d5d163e0e6d4ed27f90d89ece
tree2e6364524a8ca7e5cb3045c7b77f8f3ea7b74ec8
parent9be276e0d45e51422a104ef2eedb29efe2d0ad39
mi: 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>
src/nvme/mi.c