Re: [PATCH v2] iio: gts-helper: add __counted_by_ptr to struct iio_gts

From: Jonathan Cameron

Date: Thu Sep 24 2026 - 22:37:52 EST


On Wed, 23 Sep 2026 19:24:50 +0000
Bill Wendling <morbo@xxxxxxxxxx> wrote:

> The compiler attribute __counted_by_ptr can be used by KASAN and UBSAN
> to detect out-of-bounds accesses to pointer fields in structs where a
> corresponding element count field is available.
>
> In "struct iio_gts", there are multiple pointer fields associated with
> an element count. This patch annotates these pointer fields with
> "__counted_by_ptr" to improve runtime safety:
>
> - 'hwgain_table': counted by 'num_hwgain'
> - 'itime_table': counted by 'num_itime'
> - 'per_time_avail_scale_tables': counted by 'num_itime'
> - 'avail_all_scales_table': counted by 'num_avail_all_scales'
> - 'avail_time_tables': counted by 'num_avail_time_tables'
>
> To ensure that the count is set correctly before any pointer is
> accessed or assigned, we update "iio_init_iio_gts()" to set the counts
> prior to assigning the pointers.
>
> Cc: codemender-patching+linux@xxxxxxxxxx
> Assisted-by: LLM
> Signed-off-by: Bill Wendling <morbo@xxxxxxxxxx>
> Acked-by: Matti Vaittinen <mazziesaccount@xxxxxxxxx>
> Reviewed-by: Gustavo A. R. Silva <gustavoars@xxxxxxxxxx>
Applied to the testing branch of iio.git. So effectively queued for 7.4 merge
window.

Thanks,

Jonathan