From: Rob Landley <landley@trommello.org>
To: Brian Gerst <bgerst@didntduck.org>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [patch] ext2_fill_super breakage
Date: Thu, 28 Mar 2002 08:46:21 -0500 [thread overview]
Message-ID: <20020328140113.B96BE4FF@merlin.webofficenow.com> (raw)
In-Reply-To: <3CA2C68E.5B8C4176@zip.com.au> <3CA31BF6.7030609@didntduck.org>
On Thursday 28 March 2002 08:34 am, Brian Gerst wrote:
> Andrew Morton wrote:
> > In 2.5.7 there is a thinko in the allocation and initialisation
> > of the fs-private superblock for ext2. It's passing the wrong type
> > to the sizeof operator (which of course gives the wrong size)
> > when allocating and clearing the memory.
> >
> > Lesson for the day: this is one of the reasons why this idiom:
> >
> > some_type *p;
> >
> > p = malloc(sizeof(*p));
> > ...
> > memset(p, 0, sizeof(*p));
> >
> > is preferable to
> >
> > some_type *p;
> >
> > p = malloc(sizeof(some_type));
> > ...
> > memset(p, 0, sizeof(some_type));
> >
> > I checked the other filesystems. They're OK (but idiomatically
> > impure). I've added a couple of defensive memsets where
> > they were missing.
>
> I'm not sure I follow you here. Compiling this code (gcc 2.96):
>
> int foo1(void) { return sizeof(struct ext2_sb_info); }
> int foo2(struct ext2_sb_info *sbi) { return sizeof(*sbi); }
>
> yields:
>
> 00000b70 <foo1>:
> b70: b8 dc 00 00 00 mov $0xdc,%eax
> b75: c3 ret
>
> 00000b80 <foo2>:
> b80: b8 dc 00 00 00 mov $0xdc,%eax
> b85: c3 ret
>
> The sizes are the same, so unless it makes a difference with another
> version of gcc then this is just a cosmetic change.
If you take the sizeof based on the type of the actual variable is using,
you're guaranteed to get the right result. (The type information is only in
one place, so what the compiler's using and what you're reserving space for
have to be the same thing.)
If you sizeof( ) the type and declare the type of the variable seperately,
you have the possibility of version skew between them. The sizeof( ) and the
variable declaration itself may not match. Hence bugs.
Rob
next prev parent reply other threads:[~2002-03-28 13:46 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-03-28 7:30 Andrew Morton
2002-03-28 13:34 ` Brian Gerst
2002-03-28 13:46 ` Rob Landley [this message]
2002-03-28 13:50 ` Jos Hulzink
2002-03-28 17:26 ` Bill Davidsen
2002-03-28 17:27 ` Andrew Morton
2002-03-28 18:13 ` Brian Gerst
2002-03-28 14:21 ` Alexander Viro
2002-03-28 14:36 ` Nikita Danilov
2002-03-28 14:48 ` Alexander Viro
2002-03-28 14:51 ` Nikita Danilov
2002-03-28 15:20 ` Alexander Viro
2002-03-28 14:50 ` Arjan van de Ven
2002-03-28 15:01 ` Nikita Danilov
2002-03-28 17:45 ` Andrew Morton
2002-03-28 23:51 ` Alexander Viro
2002-03-29 0:25 ` Andrew Morton
2002-03-29 5:14 ` Andreas Dilger
2002-03-29 8:06 ` Guest section DW
2002-03-29 15:45 ` Bill Davidsen
2002-03-29 0:42 ` Bill Davidsen
2002-03-28 22:45 ` Brian Gerst
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20020328140113.B96BE4FF@merlin.webofficenow.com \
--to=landley@trommello.org \
--cc=bgerst@didntduck.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®