iio: core: Print error and fail iio_device_register() in case sample bits do not...
authorMarek Vasut <marex@denx.de>
Mon, 28 Mar 2022 19:53:07 +0000 (21:53 +0200)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Mon, 4 Apr 2022 08:11:25 +0000 (09:11 +0100)
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>
drivers/iio/industrialio-buffer.c

index 9d2c2d1..4706d0e 100644 (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)