cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [cocci] [PATCH] mmc: mmc_spi: Adjust error handling in mmc_spi_probe()
@ 2023-12-27 11:50 Markus Elfring
       [not found] ` <ZYxXTVpLfI-mgxF4@smile.fi.intel.com>
  0 siblings, 1 reply; 2+ messages in thread
From: Markus Elfring @ 2023-12-27 11:50 UTC (permalink / raw)
  To: linux-mmc, kernel-janitors, Andy Shevchenko, Dmitry Torokhov,
	Greg Kroah-Hartman, Paolo Abeni, Ulf Hansson, Yang Yingliang
  Cc: LKML, cocci

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 27 Dec 2023 12:23:20 +0100

The kfree() function was called in one case by
the mmc_spi_probe() function during error handling
even if the passed variable contained a null pointer.
This issue was detected by using the Coccinelle software.

* Thus return directly after a call of the function “kmalloc” failed
  at the beginning.

* Move an error code assignment into an if branch.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/mmc/host/mmc_spi.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
index b0cccef4cfbf..6e7d8e1e6f38 100644
--- a/drivers/mmc/host/mmc_spi.c
+++ b/drivers/mmc/host/mmc_spi.c
@@ -1349,15 +1349,17 @@ static int mmc_spi_probe(struct spi_device *spi)
 	 * NOTE if many systems use more than one MMC-over-SPI connector
 	 * it'd save some memory to share this.  That's evidently rare.
 	 */
-	status = -ENOMEM;
 	ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);
 	if (!ones)
-		goto nomem;
+		return -ENOMEM;
+
 	memset(ones, 0xff, MMC_SPI_BLOCKSIZE);

 	mmc = mmc_alloc_host(sizeof(*host), &spi->dev);
-	if (!mmc)
+	if (!mmc) {
+		status = -ENOMEM;
 		goto nomem;
+	}

 	mmc->ops = &mmc_spi_ops;
 	mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
--
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [cocci] mmc: mmc_spi: Adjust error handling in mmc_spi_probe()
       [not found] ` <ZYxXTVpLfI-mgxF4@smile.fi.intel.com>
@ 2023-12-27 17:28   ` Markus Elfring
  0 siblings, 0 replies; 2+ messages in thread
From: Markus Elfring @ 2023-12-27 17:28 UTC (permalink / raw)
  To: Andy Shevchenko, linux-mmc
  Cc: kernel-janitors, Dmitry Torokhov, Greg Kroah-Hartman,
	Paolo Abeni, Ulf Hansson, Yang Yingliang, LKML, cocci

>> The kfree() function was called in one case by
>> the mmc_spi_probe() function during error handling
>> even if the passed variable contained a null pointer.
>> This issue was detected by using the Coccinelle software.
>
>> * Thus return directly after a call of the function “kmalloc” failed
>>   at the beginning.
>>
>> * Move an error code assignment into an if branch.
>
> How is this one better?

I suggest to avoid a bit of redundant data processing also at this source code place.

Regards,
Markus

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-12-27 17:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-27 11:50 [cocci] [PATCH] mmc: mmc_spi: Adjust error handling in mmc_spi_probe() Markus Elfring
     [not found] ` <ZYxXTVpLfI-mgxF4@smile.fi.intel.com>
2023-12-27 17:28   ` [cocci] " Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).