Re: [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs
On Tue, Apr 20, 2021 at 06:07:06PM +0300, Andy Shevchenko wrote: > On Tue, Apr 20, 2021 at 03:56:16PM +0100, Mark Brown wrote: > > On Tue, Apr 20, 2021 at 05:10:04PM +0300, Andy Shevchenko wrote: > > > ffz(), that has been used to count unused native CSs, might produce UB > > Bit of an IA there... > UB -- undefined behaviour. > I'll decode it. Should I decode CS as well? CS is fine. signature.asc Description: PGP signature
Re: [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs
On Tue, Apr 20, 2021 at 03:56:16PM +0100, Mark Brown wrote: > On Tue, Apr 20, 2021 at 05:10:04PM +0300, Andy Shevchenko wrote: > > ffz(), that has been used to count unused native CSs, might produce UB > > Bit of an IA there... UB -- undefined behaviour. I'll decode it. Should I decode CS as well? -- With Best Regards, Andy Shevchenko
Re: [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs
On Tue, Apr 20, 2021 at 05:10:04PM +0300, Andy Shevchenko wrote: > ffz(), that has been used to count unused native CSs, might produce UB Bit of an IA there... signature.asc Description: PGP signature
[PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs
ffz(), that has been used to count unused native CSs, might produce UB when called against ~0U. To fix that, open code it with ffs(~value) - 1. Fixes: 7d93aecdb58d ("spi: Add generic support for unused native cs with cs-gpios") Signed-off-by: Andy Shevchenko --- drivers/spi/spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 9c3730a9f7d5..01f95bee2ac8 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2609,7 +2609,7 @@ static int spi_get_gpio_descs(struct spi_controller *ctlr) native_cs_mask |= BIT(i); } - ctlr->unused_native_cs = ffz(native_cs_mask); + ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { -- 2.30.2