diff mbox series

[v4,3/8] iio: core: NULLify private pointer when there is no private data

Message ID 20240228204919.3680786-4-andriy.shevchenko@linux.intel.com (mailing list archive)
State Changes Requested
Headers show
Series iio: core: New macros and making use of them | expand

Commit Message

Andy Shevchenko Feb. 28, 2024, 8:41 p.m. UTC
In iio_device_alloc() when size of the private data is 0,
the private pointer is calculated to behind the valid data.
NULLify it for good.

Fixes: 6d4ebd565d15 ("iio: core: wrap IIO device into an iio_dev_opaque object")
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/iio/industrialio-core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

David Lechner Feb. 28, 2024, 9:06 p.m. UTC | #1
On Wed, Feb 28, 2024 at 2:50 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> In iio_device_alloc() when size of the private data is 0,
> the private pointer is calculated to behind the valid data.
> NULLify it for good.
>
> Fixes: 6d4ebd565d15 ("iio: core: wrap IIO device into an iio_dev_opaque object")
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/iio/industrialio-core.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 4302093b92c7..bd305fa87093 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -1654,8 +1654,12 @@ 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(struct iio_dev_opaque), IIO_DMA_MINALIGN);
> +       else
> +               indio_dev->priv = NULL;

Do we actually need the else branch here since we use kzalloc() and
therefore indio_dev->priv should already be NULL?

>
>         indio_dev->dev.parent = parent;
>         indio_dev->dev.type = &iio_device_type;
> --
> 2.43.0.rc1.1.gbec44491f096
>
>
Andy Shevchenko Feb. 28, 2024, 9:36 p.m. UTC | #2
On Wed, Feb 28, 2024 at 03:06:42PM -0600, David Lechner wrote:
> On Wed, Feb 28, 2024 at 2:50 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:

...

> > -       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(struct iio_dev_opaque), IIO_DMA_MINALIGN);
> > +       else
> > +               indio_dev->priv = NULL;
> 
> Do we actually need the else branch here since we use kzalloc() and
> therefore indio_dev->priv should already be NULL?

This is more robust, but I'm okay to drop this. Up to Jonathan.
Jonathan Cameron March 3, 2024, 12:46 p.m. UTC | #3
On Wed, 28 Feb 2024 23:36:43 +0200
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> On Wed, Feb 28, 2024 at 03:06:42PM -0600, David Lechner wrote:
> > On Wed, Feb 28, 2024 at 2:50 PM Andy Shevchenko
> > <andriy.shevchenko@linux.intel.com> wrote:  
> 
> ...
> 
> > > -       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(struct iio_dev_opaque), IIO_DMA_MINALIGN);
> > > +       else
> > > +               indio_dev->priv = NULL;  
> > 
> > Do we actually need the else branch here since we use kzalloc() and
> > therefore indio_dev->priv should already be NULL?  
> 
> This is more robust, but I'm okay to drop this. Up to Jonathan.
> 

Given the allocation is just above here fine to drop the else in this
I think.
diff mbox series

Patch

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 4302093b92c7..bd305fa87093 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1654,8 +1654,12 @@  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(struct iio_dev_opaque), IIO_DMA_MINALIGN);
+	else
+		indio_dev->priv = NULL;
 
 	indio_dev->dev.parent = parent;
 	indio_dev->dev.type = &iio_device_type;