From e27cd6791de6eb68a123107586df982e7b51bbae Mon Sep 17 00:00:00 2001 From: Kees Bakker Date: Thu, 17 Oct 2024 21:54:47 +0200 Subject: [PATCH] staging: gpib: avoid unintended sign extension The code was basically like this (assuming size_t can be u64) var_u64 |= var_u8 << 24 var_u8 is first promoted to i32 and then the shift is done. Next, it is promoted to u64 by first signextending to 64 bits. This is very unlikely what was intended. So now it is first forced to u32. var_u64 |= (u32)var_u8 << 24 This was detected by Coverity, CID 1600792. Fixes: 4c41fe886a56 ("staging: gpib: Add Agilent/Keysight 82357x USB GPIB driver") Signed-off-by: Kees Bakker Link: https://lore.kernel.org/r/20241108201207.1194F18DDF5@bout3.ijzerbout.nl Signed-off-by: Greg Kroah-Hartman --- drivers/staging/gpib/agilent_82357a/agilent_82357a.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/staging/gpib/agilent_82357a/agilent_82357a.c b/drivers/staging/gpib/agilent_82357a/agilent_82357a.c index a6b177d7f8a0b..bf05fb4a736b3 100644 --- a/drivers/staging/gpib/agilent_82357a/agilent_82357a.c +++ b/drivers/staging/gpib/agilent_82357a/agilent_82357a.c @@ -664,10 +664,10 @@ static ssize_t agilent_82357a_generic_write(gpib_board_t *board, uint8_t *buffer kfree(status_data); return -EIO; } - *bytes_written = status_data[2]; - *bytes_written |= status_data[3] << 8; - *bytes_written |= status_data[4] << 16; - *bytes_written |= status_data[5] << 24; + *bytes_written = (u32)status_data[2]; + *bytes_written |= (u32)status_data[3] << 8; + *bytes_written |= (u32)status_data[4] << 16; + *bytes_written |= (u32)status_data[5] << 24; kfree(status_data); return 0; -- 2.50.1