[U-Boot] [PATCH] part_efi: fix protective_mbr struct allocation

Albert ARIBAUD albert.u.boot at aribaud.net
Wed Feb 12 18:58:37 CET 2014


Hi Fabio,

On Wed, 12 Feb 2014 15:33:17 -0200, Fabio Estevam <festevam at gmail.com>
wrote:

> Hi Albert,
> 
> On Wed, Feb 12, 2014 at 2:33 PM, Albert ARIBAUD
> <albert.u.boot at aribaud.net> wrote:
> 
> >> What about:
> >>
> >> p_mbr = calloc(1, sizeof(*p_mbr)) ?
> >
> > I don't like the idea of setting p_mbr based on *p_mbr at a time where
> > p_mbr is still undefined. I know that from a C standard perspective
> > this is ok, but I'd rather simply not run any risk and pass sizeof
> > the struct type, not a (non-existent) dereferenced 'value'.
> 
> At least in kernel this is the preferred way.
> 
> According to Documentation/CodingStyle:
> 
> "Chapter 14: Allocating memory
> 
> The preferred form for passing a size of a struct is the following:
> 
>     p = kmalloc(sizeof(*p), ...);
> 
> The alternative form where struct name is spelled out hurts readability and
> introduces an opportunity for a bug when the pointer variable type is changed
> but the corresponding sizeof that is passed to a memory allocator is not."

I still don't like it, but it makes sense indeed.

> Regards,
> 
> Fabio Estevam

Amicalement,
-- 
Albert.


More information about the U-Boot mailing list