Commit fd542c5e authored by Marek Vasut's avatar Marek Vasut Committed by Jonathan Cameron

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: default avatarMarek Vasut <marex@denx.de>
Cc: Daniel Baluta <daniel.baluta@nxp.com>
Reviewed-by: default avatarNuno Sá <nuno.sa@analog.com>
Reviewed-by: default avatarAndy Shevchenko <andy@kernel.org>
Link: https://lore.kernel.org/r/20220328195307.154422-1-marex@denx.deSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 94059c52
......@@ -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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment