Re: [PATCH] iio: st_sensors: add __counted_by_ptr to struct st_sensor_settings
From: Nuno Sá
Date: Tue Sep 22 2026 - 10:44:25 EST
On Tue, Sep 22, 2026 at 11:21:11AM +0000, Bill Wendling wrote:
> Annotate the "ch" pointer member of "struct st_sensor_settings" with the
> "__counted_by_ptr" attribute. The elements of "ch" are counted by the
> "num_ch" member in the same struct.
>
> All instances of "struct st_sensor_settings" are defined as "static
> const" arrays across the ST sensor core drivers. For pressure sensors,
> "num_ch" is explicitly initialized with the size of the respective
> channel array using "ARRAY_SIZE(...)" during static definition.
>
> For accelerometer, gyroscope, and magnetometer sensors, "num_ch" is not
> explicitly initialized (and thus defaults to 0). This is because those
> drivers hardcode the channel count to "ST_SENSORS_NUMBER_ALL_CHANNELS"
> rather than using "num_ch" from the settings struct.
>
> Since these structures are static const, both "ch" and "num_ch" are
> fully initialized at compile time and available immediately at boot
> time. The only accesses to the "ch" field of "st_sensor_settings" occur
> when assigning it to "indio_dev->channels" during device probing.
>
> Because "sensor_settings->ch" is never dereferenced or accessed as an
> array, adding the "__counted_by_ptr" annotation does not cause any
> runtime panics or false-positive bounds checks under KASAN or UBSAN.
>
> Cc: codemender-patching+linux@xxxxxxxxxx
> Assisted-by: LLM
> Signed-off-by: Bill Wendling <morbo@xxxxxxxxxx>
> ---
Reviewed-by: Nuno Sá <nuno.sa@xxxxxxxxxx>
> include/linux/iio/common/st_sensors.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/iio/common/st_sensors.h b/include/linux/iio/common/st_sensors.h
> index 1ba496f0fea5..6e914ed49529 100644
> --- a/include/linux/iio/common/st_sensors.h
> +++ b/include/linux/iio/common/st_sensors.h
> @@ -206,7 +206,7 @@ struct st_sensor_settings {
> u8 wai;
> u8 wai_addr;
> char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
> - struct iio_chan_spec *ch;
> + struct iio_chan_spec *ch __counted_by_ptr(num_ch);
> int num_ch;
> struct st_sensor_odr odr;
> struct st_sensor_power pw;
> --
> 2.55.0.1082.g2b9226bbc0-goog
>