mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Nick Desaulniers <nick.desaulniers@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>,
	Deepa Dinamani <deepa.kernel@gmail.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: precedence bug in MAKE_PROCESS_CPUCLOCK macro?
Date: Thu, 28 Dec 2017 16:49:16 +0100 (CET)	[thread overview]
Message-ID: <alpine.DEB.2.20.1712281645540.1899@nanos> (raw)
In-Reply-To: <CAH7mPvgzy+DRUoqNMnLqSLQZaWquodTmUe3=m3ZG=CqpL7cBzw@mail.gmail.com>

On Sat, 23 Dec 2017, Nick Desaulniers wrote:
> I'm seeing the following warning compiling with Clang:
> 
> kernel/time/posix-cpu-timers.c:1397:29: warning: shifting a negative
> signed value is undefined
>       [-Wshift-negative-value]
>         return posix_cpu_clock_get(THREAD_CLOCK, tp);
>                                    ^~~~~~~~~~~~
> kernel/time/posix-cpu-timers.c:1367:22: note: expanded from macro 'THREAD_CLOCK'
> #define THREAD_CLOCK    MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
>                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ./include/linux/posix-timers.h:48:2: note: expanded from macro
> 'MAKE_THREAD_CPUCLOCK'
>         MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)
>         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ./include/linux/posix-timers.h:46:23: note: expanded from macro
> 'MAKE_PROCESS_CPUCLOCK'
>         ((~(clockid_t) (pid) << 3) | (clockid_t) (clock))
>           ~~~~~~~~~~~~~~~~~~ ^
> 
> If I understand C's operator precedence rules
> (http://en.cppreference.com/w/c/language/operator_precedence)
> correctly, then I suspect the problem is in the sub-expression:
> 
> (~(clockid_t) (pid) << 3)
> 
> where pid (an argument to the macro) is first cast to a clockid_t (aka
> [signed] int), then negated, then shifted by 3 (oops, undefined
> behavior).
> 
> Should the result after negation be cast to an unsigned int, or should
> the left shift happen before negation?
> 
> CPUCLOCK_PID and CLOCKID_TO_FD seem to shift then negate, but
> FD_TO_CLOCKID seems to have the same issue as MAKE_PROCESS_CPUCLOCK.
> 
> Changing the sub-expression to:
> 
> (~(clockid_t) ((pid) << 3))
> 
> changes what it evaluates to.  Changing it to:
> 
> (~(unsigned) (pid) << 3)
> 
> or
> 
> ((unsigned) ~(clockid_t) (pid) << 3)
> 
> or
> 
> (((unsigned) ~(clockid_t) (pid)) << 3) /* ugly */

All of these are butt ugly. And the same problem exists for FD_TO_CLOCKID.

My preference would be to replace all these crappy macros with simple
inline functions.

Thanks,

	tglx

  reply	other threads:[~2017-12-28 15:49 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-24  3:26 Nick Desaulniers
2017-12-28 15:49 ` Thomas Gleixner [this message]
2017-12-29  3:01   ` [PATCH] posix-timers: prevent UB from shifting negative signed value Nick Desaulniers
2017-12-29  3:08     ` Nick Desaulniers
2017-12-29  3:11     ` [PATCH v2] " Nick Desaulniers
2018-01-04 14:01       ` [tip:timers/core] posix-timers: Prevent " tip-bot for Nick Desaulniers

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=alpine.DEB.2.20.1712281645540.1899@nanos \
    --to=tglx@linutronix.de \
    --cc=deepa.kernel@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nick.desaulniers@gmail.com \
    --cc=viro@zeniv.linux.org.uk \
    /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®