Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits. Signed-off-by: Marek Vasut <marex@xxxxxxx> Cc: Andy Shevchenko <andy@xxxxxxxxxx> Cc: Daniel Baluta <daniel.baluta@xxxxxxx> Cc: Jonathan Cameron <Jonathan.Cameron@xxxxxxxxxx> --- V2: Use dev_err() instead as WARN_ON() may panic() the kernel on existing machines V3: Abort probe and return -EINVAL in case this condition is triggered --- drivers/iio/industrialio-buffer.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c index b078eb2f3c9de..75a1c57b49102 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -1629,6 +1629,19 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer, if (channels[i].scan_index < 0) continue; + /* Verify that sample bits fit into storage */ + if (channels[i].scan_type.storagebits < + channels[i].scan_type.realbits + + channels[i].scan_type.shift) { + dev_err(&indio_dev->dev, + "Channel %d storagebits (%d) < shifted realbits (%d + %d)\n", + i, channels[i].scan_type.storagebits, + channels[i].scan_type.realbits, + channels[i].scan_type.shift); + ret = -EINVAL; + goto error_cleanup_dynamic; + } + ret = iio_buffer_add_channel_sysfs(indio_dev, buffer, &channels[i]); if (ret < 0) -- 2.35.1