Commit 499a8cee authored by Antoniu Miclaus's avatar Antoniu Miclaus Committed by Jonathan Cameron
Browse files

iio: adc: ad4851: fix ad4858 chan pointer handling



The pointer returned from ad4851_parse_channels_common() is incremented
internally as each channel is populated. In ad4858_parse_channels(),
the same pointer was further incremented while setting ext_scan_type
fields for each channel. This resulted in indio_dev->channels being set
to a pointer past the end of the allocated array, potentially causing
memory corruption or undefined behavior.

Fix this by iterating over the channels using an explicit index instead
of incrementing the pointer. This preserves the original base pointer
and ensures all channel metadata is set correctly.

Fixes: 6250803f ("iio: adc: ad4851: add ad485x driver")
Signed-off-by: default avatarAntoniu Miclaus <antoniu.miclaus@analog.com>
Reviewed-by: default avatarNuno Sá <nuno.sa@analog.com>
Link: https://patch.msgid.link/20250509101657.6742-1-antoniu.miclaus@analog.com


Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent e2f82001
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -1034,7 +1034,7 @@ static int ad4858_parse_channels(struct iio_dev *indio_dev)
	struct device *dev = &st->spi->dev;
	struct iio_chan_spec *ad4851_channels;
	const struct iio_chan_spec ad4851_chan = AD4858_IIO_CHANNEL;
	int ret;
	int ret, i = 0;

	ret = ad4851_parse_channels_common(indio_dev, &ad4851_channels,
					   ad4851_chan);
@@ -1042,15 +1042,15 @@ static int ad4858_parse_channels(struct iio_dev *indio_dev)
		return ret;

	device_for_each_child_node_scoped(dev, child) {
		ad4851_channels->has_ext_scan_type = 1;
		ad4851_channels[i].has_ext_scan_type = 1;
		if (fwnode_property_read_bool(child, "bipolar")) {
			ad4851_channels->ext_scan_type = ad4851_scan_type_20_b;
			ad4851_channels->num_ext_scan_type = ARRAY_SIZE(ad4851_scan_type_20_b);
			ad4851_channels[i].ext_scan_type = ad4851_scan_type_20_b;
			ad4851_channels[i].num_ext_scan_type = ARRAY_SIZE(ad4851_scan_type_20_b);
		} else {
			ad4851_channels->ext_scan_type = ad4851_scan_type_20_u;
			ad4851_channels->num_ext_scan_type = ARRAY_SIZE(ad4851_scan_type_20_u);
			ad4851_channels[i].ext_scan_type = ad4851_scan_type_20_u;
			ad4851_channels[i].num_ext_scan_type = ARRAY_SIZE(ad4851_scan_type_20_u);
		}
		ad4851_channels++;
		i++;
	}

	indio_dev->channels = ad4851_channels;