iio: hmc5843_spi: fix a NULL pointer dereference
PATCH 1/1 iio: hmc5843_spi: fix a NULL pointer dereference
In case devm_regmap_init_spi fails and returns NULL, the fix returns an error to avoid NULL pointer dereference
--- drivers/iio/magnetometer/hmc5843_spi.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/iio/magnetometer/hmc5843_spi.c +7 -1
@@ -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); }
Signature · 1 line
2.17.1
Jonathan Cameron
maintainerQuoted 4 lines · On Sat, 9 Mar 2019 12:56:27 -0600 Kangjie Lu <[email protected]> wrote:
In case
devm_regmap_init_spifails and returns NULL, the fix returns an error to avoid NULL pointer dereferenceSigned-off-by Kangjie Lu <[email protected]>
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
Quoted 9 lines
--- drivers/iio/magnetometer/hmc5843_spi.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/iio/magnetometer/hmc5843_spi.c +0
-0
drivers/iio/magnetometer/hmc5843_spi.c static int hmc5843_spi_probe(struct spi_device *spi) · line 63
Quoted 5 lines
drivers/iio/magnetometer/hmc5843_spi.c +2
-0@@ -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);
Quoted 16 lines
drivers/iio/magnetometer/hmc5843_spi.c +5
-1+ +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); }