linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: hmc5843_spi: fix a NULL pointer dereference
@ 2019-03-09 18:56 Kangjie Lu
  2019-03-10 10:26 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Kangjie Lu @ 2019-03-09 18:56 UTC (permalink / raw)
  To: kjlu
  Cc: pakki001, Jonathan Cameron, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio, linux-kernel

In case devm_regmap_init_spi fails and returns NULL, the fix
returns an error to avoid NULL pointer dereference

Signed-off-by: Kangjie Lu <kjlu@umn.edu>
---
 drivers/iio/magnetometer/hmc5843_spi.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/magnetometer/hmc5843_spi.c b/drivers/iio/magnetometer/hmc5843_spi.c
index 535f03a70d63..15e2f7cbb3b5 100644
--- a/drivers/iio/magnetometer/hmc5843_spi.c
+++ b/drivers/iio/magnetometer/hmc5843_spi.c
@@ -59,6 +59,12 @@ static int hmc5843_spi_probe(struct spi_device *spi)
 {
 	int ret;
 	const struct spi_device_id *id = spi_get_device_id(spi);
+	struct regmap *devm_regmap = devm_regmap_init_spi(spi,
+			&hmc5843_spi_regmap_config);
+
+	if (IS_ERR(devm_regmap))
+		return PTR_ERR(devm_regmap);
+
 
 	spi->mode = SPI_MODE_3;
 	spi->max_speed_hz = 8000000;
@@ -68,7 +74,7 @@ static int hmc5843_spi_probe(struct spi_device *spi)
 		return ret;
 
 	return hmc5843_common_probe(&spi->dev,
-			devm_regmap_init_spi(spi, &hmc5843_spi_regmap_config),
+			devm_regmap,
 			id->driver_data, id->name);
 }
 
-- 
2.17.1


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

* Re: [PATCH] iio: hmc5843_spi: fix a NULL pointer dereference
  2019-03-09 18:56 [PATCH] iio: hmc5843_spi: fix a NULL pointer dereference Kangjie Lu
@ 2019-03-10 10:26 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2019-03-10 10:26 UTC (permalink / raw)
  To: Kangjie Lu
  Cc: pakki001, Hartmut Knaack, Lars-Peter Clausen,
	Peter Meerwald-Stadler, linux-iio, linux-kernel

On Sat,  9 Mar 2019 12:56:27 -0600
Kangjie Lu <kjlu@umn.edu> wrote:

> In case devm_regmap_init_spi fails and returns NULL, the fix
> returns an error to avoid NULL pointer dereference
> 
> Signed-off-by: Kangjie Lu <kjlu@umn.edu>

Hi.

The fix is good, but please do the same for the i2c element of
the driver. I'd like them both in a single patch as it's exactly
the same issue in the same driver.

Minor comment inline on how I think it could be a tiny bit
nicer.

Thanks,

Jonathan

> ---
>  drivers/iio/magnetometer/hmc5843_spi.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/magnetometer/hmc5843_spi.c b/drivers/iio/magnetometer/hmc5843_spi.c
> index 535f03a70d63..15e2f7cbb3b5 100644
> --- a/drivers/iio/magnetometer/hmc5843_spi.c
> +++ b/drivers/iio/magnetometer/hmc5843_spi.c
> @@ -59,6 +59,12 @@ static int hmc5843_spi_probe(struct spi_device *spi)
>  {
>  	int ret;
>  	const struct spi_device_id *id = spi_get_device_id(spi);
> +	struct regmap *devm_regmap = devm_regmap_init_spi(spi,
> +			&hmc5843_spi_regmap_config);
Split this into the variable instantiation and the assignment.
That will let you (I think) avoid the nasty alignement.
Also, no need to have the devm_ prefix on the variable name.

	struct regmap *regmap;

	regmap = devm_regmap_init(spi, &hmc5843_spi_regmap_config);

> +
> +	if (IS_ERR(devm_regmap))
> +		return PTR_ERR(devm_regmap);
> +
>  
>  	spi->mode = SPI_MODE_3;
>  	spi->max_speed_hz = 8000000;
> @@ -68,7 +74,7 @@ static int hmc5843_spi_probe(struct spi_device *spi)
>  		return ret;
>  
>  	return hmc5843_common_probe(&spi->dev,
> -			devm_regmap_init_spi(spi, &hmc5843_spi_regmap_config),
> +			devm_regmap,
>  			id->driver_data, id->name);
>  }
>  


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

end of thread, other threads:[~2019-03-10 10:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-09 18:56 [PATCH] iio: hmc5843_spi: fix a NULL pointer dereference Kangjie Lu
2019-03-10 10:26 ` Jonathan Cameron

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).