mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Al Viro <viro@ZenIV.linux.org.uk>
To: Aleksa Sarai <cyphar@cyphar.com>
Cc: xypron.glpk@gmx.de, Tejun Heo <tj@kernel.org>,
	koct9i@gmail.com, ebiederm@xmission.com,
	Wei Tang <tangwei@cmss.chinamobile.com>,
	linux-kernel@vger.kernel.org, oleg@redhat.com,
	josh@joshtriplett.org, jason.low2@hp.com, peterz@infradead.org,
	Ingo Molnar <mingo@kernel.org>,
	akpm@linux-foundation.org, kirill.shutemov@linux.intel.com
Subject: Re: [PATCH] kernel/fork.c: use sizeof() instead of sizeof
Date: Tue, 2 Feb 2016 17:04:06 +0000	[thread overview]
Message-ID: <20160202170406.GP17997@ZenIV.linux.org.uk> (raw)
In-Reply-To: <CAOviyajXB2y5zuFhdRPQfVSe_NYvaFjjUz6f86e791AvFPYfMw@mail.gmail.com>

On Tue, Feb 02, 2016 at 03:56:01PM +0000, Aleksa Sarai wrote:

> I haven't looked at the order of operations for sizeof, but I imagine
> there's cases where it might bind in a different way than is expected. Are
> you sure there'd be no negative downside to removing the check (the whole
> point is to ensure no new code has stuff like that).

I won't comment on the whole point (or lack thereof) of checkpatch.pl, but
the only subtlety with sizeof is that sizeof ( type-name ) <something> is
*never* interpreted as sizeof of something cast to type-name.  IOW, its
priority is higher than that of typecasts.  If <something> starts with {,
it's a sizeof of compound literal, otherwise it's an unary expression
"sizeof ( type-name )" followed by <something>, which might or might not
yield a valid expression (e.g.
	sizeof(int)1
won't parse, while
	sizeof(int)-1
will be treated as (sizeof(int)) - 1).

Potential headache is along the lines of
#define A (int)-1
sizeof A
but that's more of "use enough parentheses in body of a macro to avoid nasty
surprises" - same as e.g.
#define A 2 + 2
A * A
yielding 8 (2 + 2 * 2 + 2) rather than expected 16 ((2 + 2) * (2 + 2))

FWIW, the actual rules are
	unary-expression: postfix-expression |
			  ++ unary-expression |
			  -- unary-expression |
			  - cast-expression |
			  + cast-expression |
			  ! cast-expression |
			  ~ cast-expression |
			  * cast-expression |
			  & cast-expression |
			  sizeof unary-expression |
			  sizeof ( type-name )
	cast-expression: unary-expression |
			 ( type-name ) cast-expression
Note that while e.g.
	++ ++ n
is allowed by grammar, it runs afoul of the constraint for ++ argument, which
must be a modifiable lvalue.  None of the operators above yield that, so
the rules for ++ and -- might as well have been ++ postfix-expression and
-- postfix-expression resp.

  parent reply	other threads:[~2016-02-02 17:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-02  7:53 Wei Tang
2016-02-02  9:11 ` Ingo Molnar
2016-02-02 11:28   ` [PATCH] treewide: Use 'sizeof(x)' instead of 'sizeof x' Ingo Molnar
2016-02-02 13:58     ` Joe Perches
2016-02-02 15:45   ` [PATCH] kernel/fork.c: use sizeof() instead of sizeof Al Viro
2016-02-02 17:02     ` Joe Perches
2016-02-02 17:11       ` Al Viro
2016-02-02 17:21         ` Joe Perches
     [not found]     ` <CAOviyajXB2y5zuFhdRPQfVSe_NYvaFjjUz6f86e791AvFPYfMw@mail.gmail.com>
2016-02-02 17:04       ` Al Viro [this message]
2016-02-03  3:46         ` Kevin Easton
2016-02-03  4:02           ` Al Viro

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=20160202170406.GP17997@ZenIV.linux.org.uk \
    --to=viro@zeniv.linux.org.uk \
    --cc=akpm@linux-foundation.org \
    --cc=cyphar@cyphar.com \
    --cc=ebiederm@xmission.com \
    --cc=jason.low2@hp.com \
    --cc=josh@joshtriplett.org \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=koct9i@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tangwei@cmss.chinamobile.com \
    --cc=tj@kernel.org \
    --cc=xypron.glpk@gmx.de \
    /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

Powered by JetHome