Re: [PATCH] futex: avoid undefined behaviour when shift exponent is negative

From: Thomas Gleixner
Date: Thu Jun 29 2017 - 02:34:36 EST


On Thu, 29 Jun 2017, zhong jiang wrote:
> On 2017/6/29 6:13, Thomas Gleixner wrote:
> > That's simply wrong. If oparg is negative and the SHIFT bit is set then the
> > result is undefined today and there is no way that this can be used at
> > all.
> >
> > On x86:
> >
> > 1 << -1 = 0x80000000
> > 1 << -2048 = 0x00000001
> > 1 << -2047 = 0x00000002
> but I test the cases in x86_64 all is zero. I wonder whether it is related to gcc or not
>
> zj.c:15:8: warning: left shift count is negative [-Wshift-count-negative]
> j = 1 << -2048;
> ^
> [root@localhost zhongjiang]# ./zj
> j = 0

Which is not a surprise because the compiler can detect it as the shift is
a constant. oparg is not so constant ...

Thanks,

tglx