2024-03-04 15:29:45

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 1/2] iio: core: Leave private pointer NULL when no private data supplied

In iio_device_alloc() when size of the private data is 0,
the private pointer is calculated to point behind the valid data.
Leave it NULL when no private data supplied.

Fixes: 6d4ebd565d15 ("iio: core: wrap IIO device into an iio_dev_opaque object")
Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/iio/industrialio-core.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 4302093b92c7..8684ba246969 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1654,8 +1654,10 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
return NULL;

indio_dev = &iio_dev_opaque->indio_dev;
- indio_dev->priv = (char *)iio_dev_opaque +
- ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN);
+
+ if (sizeof_priv)
+ indio_dev->priv = (char *)iio_dev_opaque +
+ ALIGN(sizeof(*iio_dev_opaque), IIO_DMA_MINALIGN);

indio_dev->dev.parent = parent;
indio_dev->dev.type = &iio_device_type;
--
2.43.0.rc1.1.gbec44491f096



2024-03-09 18:37:20

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v1 1/2] iio: core: Leave private pointer NULL when no private data supplied

On Mon, 4 Mar 2024 16:04:32 +0200
Andy Shevchenko <[email protected]> wrote:

> In iio_device_alloc() when size of the private data is 0,
> the private pointer is calculated to point behind the valid data.
> Leave it NULL when no private data supplied.
>
> Fixes: 6d4ebd565d15 ("iio: core: wrap IIO device into an iio_dev_opaque object")
> Signed-off-by: Andy Shevchenko <[email protected]>

Fix is a strong word given any driver trying to access the pointer in this case
will be broken anyway. I don't mind it being backported as it'll make bugs
easier to identify but I'm not rushing it in.

Otherwise seems like a sensible bit of tidying up.

Jonathan

> ---
> drivers/iio/industrialio-core.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 4302093b92c7..8684ba246969 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -1654,8 +1654,10 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
> return NULL;
>
> indio_dev = &iio_dev_opaque->indio_dev;
> - indio_dev->priv = (char *)iio_dev_opaque +
> - ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN);
> +
> + if (sizeof_priv)
> + indio_dev->priv = (char *)iio_dev_opaque +
> + ALIGN(sizeof(*iio_dev_opaque), IIO_DMA_MINALIGN);
>
> indio_dev->dev.parent = parent;
> indio_dev->dev.type = &iio_device_type;