iio: core: Print error and fail iio_device_register() in case sample bits do not fit storage bits

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@denx.de>
Cc: Daniel Baluta <daniel.baluta@nxp.com>
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Link: https://lore.kernel.org/r/20220328195307.154422-1-marex@denx.de
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
pull/31/merge
Marek Vasut 2022-03-28 21:53:07 +02:00 committed by Jonathan Cameron
parent 94059c5227
commit fd542c5eda
1 changed files with 13 additions and 0 deletions

View File

@ -1632,6 +1632,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)