diff mbox series

[2/8] regmap: zero out the regmap on allocation

Message ID 20200527125208.24881-3-p.yadav@ti.com
State Superseded
Delegated to: Simon Glass
Headers show
Series regmap: Add managed API, regmap fields, regmap config | expand

Commit Message

Pratyush Yadav May 27, 2020, 12:52 p.m. UTC
Some fields will be introduced in the regmap structure that should be
set to 0 by default. So, once we allocate a regmap, make sure it is
zeroed out to avoid unexpected defaults for those values.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
---
 drivers/core/regmap.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Simon Glass May 31, 2020, 2:08 p.m. UTC | #1
Hi Pratyush,

On Wed, 27 May 2020 at 06:52, Pratyush Yadav <p.yadav@ti.com> wrote:
>
> Some fields will be introduced in the regmap structure that should be
> set to 0 by default. So, once we allocate a regmap, make sure it is
> zeroed out to avoid unexpected defaults for those values.
>
> Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
> ---
>  drivers/core/regmap.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>

Reviewed-by: Simon Glass <sjg@chromium.org>

But I think you should use calloc() instead


> diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c
> index 74225361fd..24651fb3ec 100644
> --- a/drivers/core/regmap.c
> +++ b/drivers/core/regmap.c
> @@ -30,10 +30,12 @@ DECLARE_GLOBAL_DATA_PTR;
>  static struct regmap *regmap_alloc(int count)
>  {
>         struct regmap *map;
> +       size_t size = sizeof(*map) + sizeof(map->ranges[0]) * count;
>
> -       map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
> +       map = malloc(size);
>         if (!map)
>                 return NULL;
> +       memset(map, 0, size);
>         map->range_count = count;
>
>         return map;
> --
> 2.26.2
>
Pratyush Yadav June 4, 2020, 2:43 p.m. UTC | #2
Hi Simon,

On 31/05/20 08:08AM, Simon Glass wrote:
> Hi Pratyush,
> 
> On Wed, 27 May 2020 at 06:52, Pratyush Yadav <p.yadav@ti.com> wrote:
> >
> > Some fields will be introduced in the regmap structure that should be
> > set to 0 by default. So, once we allocate a regmap, make sure it is
> > zeroed out to avoid unexpected defaults for those values.
> >
> > Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
> > ---
> >  drivers/core/regmap.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> >
> 
> Reviewed-by: Simon Glass <sjg@chromium.org>
> 
> But I think you should use calloc() instead

Ok. Will do. FWIW, I don't see a clear separation of the total size into 
elements and size of each element so I think doing something like 
calloc(1, size) is a bit strange.
 
> > diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c
> > index 74225361fd..24651fb3ec 100644
> > --- a/drivers/core/regmap.c
> > +++ b/drivers/core/regmap.c
> > @@ -30,10 +30,12 @@ DECLARE_GLOBAL_DATA_PTR;
> >  static struct regmap *regmap_alloc(int count)
> >  {
> >         struct regmap *map;
> > +       size_t size = sizeof(*map) + sizeof(map->ranges[0]) * count;
> >
> > -       map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
> > +       map = malloc(size);
> >         if (!map)
> >                 return NULL;
> > +       memset(map, 0, size);
> >         map->range_count = count;
> >
> >         return map;
diff mbox series

Patch

diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c
index 74225361fd..24651fb3ec 100644
--- a/drivers/core/regmap.c
+++ b/drivers/core/regmap.c
@@ -30,10 +30,12 @@  DECLARE_GLOBAL_DATA_PTR;
 static struct regmap *regmap_alloc(int count)
 {
 	struct regmap *map;
+	size_t size = sizeof(*map) + sizeof(map->ranges[0]) * count;
 
-	map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
+	map = malloc(size);
 	if (!map)
 		return NULL;
+	memset(map, 0, size);
 	map->range_count = count;
 
 	return map;