Re: [PATCH] rtc: tx4939: avoid unintended sign extension on a 24 bit shift

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Feb 15, 2018 at 8:59 PM, Colin King <colin.king@xxxxxxxxxxxxx> wrote:
> From: Colin Ian King <colin.king@xxxxxxxxxxxxx>
>
> The shifting of buf[5] by 24 bits to the left will be promoted to
> a 32 bit signed int and then sign-extended to an unsigned long. If
> the top bit of buf[5] is set then all then all the upper bits sec
> end up as also being set because of the sign-extension. Fix this by
> casting buf[5] to an unsigned long before the shift.
>
> Detected by CoverityScan, CID#1465292 ("Unintended sign extension")
>
> Fixes: 0e1492330cd2 ("rtc: add rtc-tx4939 driver")
> Signed-off-by: Colin Ian King <colin.king@xxxxxxxxxxxxx>

>         spin_unlock_irq(&pdata->lock);
> -       sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
> +       sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
> +               (buf[3] << 8) | buf[2];

Wouldn't be better to use le32_to_cpu() or get_unaligned()?

-- 
With Best Regards,
Andy Shevchenko



[Index of Archives]     [Linux Sound]     [ALSA Users]     [ALSA Devel]     [Linux Audio Users]     [Linux Media]     [Kernel]     [Gimp]     [Yosemite News]     [Linux Media]

  Powered by Linux