Skip to contentSkip to conversation

iio: lmp91000: fix probe order and cleanup paths

PATCH Superseded Salah Triki 2 participants 3 messages May 5 – May 6 IIO

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: lmp91000: fix probe order and cleanup paths

Fix the initialization order in lmp91000_probe where the immutable trigger is set before data->cb_buffer is initialized, which would cause a NULL pointer dereference.

Also, correct the cleanup labels in the error path to properly unregister the trigger before performing the triggered buffer cleanup, following the standard LIFO (Last-In-First-Out) resource release order.

Fixes 67e17300dc1d ("iio: potentiostat: add LMP91000 support")
---
 drivers/iio/potentiostat/lmp91000.c | 30 ++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
drivers/iio/potentiostat/lmp91000.c +15 -15
index eccc2a34358f..b4676b5b60ae 100644
@@ -330,6 +330,18 @@ static int lmp91000_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
+	data->cb_buffer = iio_channel_get_all_cb(dev, &lmp91000_buffer_cb,
+						 indio_dev);
+
+	if (IS_ERR(data->cb_buffer)) {
+		if (PTR_ERR(data->cb_buffer) == -ENODEV)
+			ret = -EPROBE_DEFER;
+		else
+			ret = PTR_ERR(data->cb_buffer);
+
+		goto error_unreg_buffer;
+	}
+
 	ret = iio_trigger_set_immutable(iio_channel_cb_get_iio_dev(data->cb_buffer),
 					data->trig);
 	if (ret) {
@@ -349,18 +361,6 @@ static int lmp91000_probe(struct i2c_client *client)
 	if (ret)
 		goto error_unreg_trigger;
 
-	data->cb_buffer = iio_channel_get_all_cb(dev, &lmp91000_buffer_cb,
-						 indio_dev);
-
-	if (IS_ERR(data->cb_buffer)) {
-		if (PTR_ERR(data->cb_buffer) == -ENODEV)
-			ret = -EPROBE_DEFER;
-		else
-			ret = PTR_ERR(data->cb_buffer);
-
-		goto error_unreg_buffer;
-	}
-
 	data->adc_chan = iio_channel_cb_get_channels(data->cb_buffer);
 
 	ret = iio_device_register(indio_dev);
@@ -372,12 +372,12 @@ static int lmp91000_probe(struct i2c_client *client)
 error_unreg_cb_buffer:
 	iio_channel_release_all_cb(data->cb_buffer);
 
-error_unreg_buffer:
-	iio_triggered_buffer_cleanup(indio_dev);
-
 error_unreg_trigger:
 	iio_trigger_unregister(data->trig);
 
+error_unreg_buffer:
+	iio_triggered_buffer_cleanup(indio_dev);
+
 	return ret;
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 1 line · Salah Triki

Fix the initialization order in lmp91000_probe where the immutable trigger

lmp91000_probe()

Quoted 6 lines

is set before data->cb_buffer is initialized, which would cause a NULL pointer dereference.

Also, correct the cleanup labels in the error path to properly unregister the trigger before performing the triggered buffer cleanup, following the standard LIFO (Last-In-First-Out) resource release order.

...

drivers/iio/potentiostat/lmp91000.c static int lmp91000_probe(struct i2c_client *client) · line 334

Quoted 2 lines
drivers/iio/potentiostat/lmp91000.c +2 -0
+	data->cb_buffer = iio_channel_get_all_cb(dev, &lmp91000_buffer_cb,
+						 indio_dev);

I would go with a single line here.

Quoted 1 line

+

Despite being in the original code, please drop this unneeded blank line.

Quoted 8 lines
drivers/iio/potentiostat/lmp91000.c +8 -0
+	if (IS_ERR(data->cb_buffer)) {
+		if (PTR_ERR(data->cb_buffer) == -ENODEV)
+			ret = -EPROBE_DEFER;
+		else
+			ret = PTR_ERR(data->cb_buffer);
+
+		goto error_unreg_buffer;
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 1 line · Salah Triki

Fix the initialization order in lmp91000_probe where the immutable trigger

lmp91000_probe()

Quoted 6 lines

is set before data->cb_buffer is initialized, which would cause a NULL pointer dereference.

Also, correct the cleanup labels in the error path to properly unregister the trigger before performing the triggered buffer cleanup, following the standard LIFO (Last-In-First-Out) resource release order.

...

Quoted 9 lines

drivers/iio/potentiostat/lmp91000.c static int lmp91000_probe(struct i2c_client *client) · line 334

Quoted 2 lines
drivers/iio/potentiostat/lmp91000.c +2 -0
+	data->cb_buffer = iio_channel_get_all_cb(dev, &lmp91000_buffer_cb,
+						 indio_dev);

I would go with a single line here.

Quoted 1 line

+

Despite being in the original code, please drop this unneeded blank line.

Quoted 8 lines

drivers/iio/potentiostat/lmp91000.c static int lmp91000_probe(struct i2c_client *client) · line 343

Quoted 8 lines
drivers/iio/potentiostat/lmp91000.c +8 -0
+	if (IS_ERR(data->cb_buffer)) {
+		if (PTR_ERR(data->cb_buffer) == -ENODEV)
+			ret = -EPROBE_DEFER;
+		else
+			ret = PTR_ERR(data->cb_buffer);
+
+		goto error_unreg_buffer;
+	}

On top of that fix the Subject as Joshua suggested in the other patch.

Signature · 2 lines
With Best Regards,
Andy Shevchenko