spi: Avoid undefined behaviour when counting unused native CSs
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Tue, 20 Apr 2021 16:44:25 +0000 (19:44 +0300)
committerMark Brown <broonie@kernel.org>
Wed, 21 Apr 2021 14:49:54 +0000 (15:49 +0100)
ffz(), that has been used to count unused native CSs,
might cause undefined behaviour 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 <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20210420164425.40287-2-andriy.shevchenko@linux.intel.com
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/spi/spi.c

index 2e5dca20c8d00b4e659637819b059f7aea9c1aa4..6fe2a9509675f5d9505c791182b6fc839122e225 100644 (file)
@@ -2611,7 +2611,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) {