From patchwork Tue Apr 20 14:10:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 425695 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3BD49C433ED for ; Tue, 20 Apr 2021 14:09:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0010A613C1 for ; Tue, 20 Apr 2021 14:09:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231482AbhDTOKY (ORCPT ); Tue, 20 Apr 2021 10:10:24 -0400 Received: from mga01.intel.com ([192.55.52.88]:23728 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231758AbhDTOKX (ORCPT ); Tue, 20 Apr 2021 10:10:23 -0400 IronPort-SDR: RLsquzDJdRXzztuUTP43U04QwuCFXN5apyf/5Tb8RqSLMe5sF3Ytv52VGDmNeRhpRaHBai/Fja FLilMUFn+4lg== X-IronPort-AV: E=McAfee;i="6200,9189,9960"; a="216102618" X-IronPort-AV: E=Sophos;i="5.82,237,1613462400"; d="scan'208";a="216102618" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Apr 2021 07:09:51 -0700 IronPort-SDR: PFU2atH3rryrxPMT9IJGqTU/Vl1E3KmeCFnpTxqKMsVVjAfXhWHOT+804ZaFB5egC4mnSXTMT+ X5A17VQI6hVg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,237,1613462400"; d="scan'208";a="391031403" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga007.fm.intel.com with ESMTP; 20 Apr 2021 07:09:50 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id CDEE31A1; Tue, 20 Apr 2021 17:10:07 +0300 (EEST) From: Andy Shevchenko To: Mark Brown , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Shevchenko Subject: [PATCH v1 2/2] spi: Avoid potential UB when counting unused native CSs Date: Tue, 20 Apr 2021 17:10:04 +0300 Message-Id: <20210420141004.59936-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210420141004.59936-1-andriy.shevchenko@linux.intel.com> References: <20210420141004.59936-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org 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) {