[cfe-commits] r59420 - /cfe/trunk/lib/AST/ExprConstant.cpp

Eli Friedman eli.friedman at gmail.com
Sun Nov 16 15:39:09 PST 2008


On Sun, Nov 16, 2008 at 2:45 PM, Anders Carlsson <andersca at mac.com> wrote:
>>> +  APSInt Index;
>>> +  if (!EvaluateInteger(E->getIdx(), Index, Info))
>>> +    return APValue();
>>> +
>>> +  uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
>>> +
>>> +  uint64_t Offset = Index.getSExtValue() * ElementSize;
>>
>> This could potentially crash once we support integers larger than 64
>> bits.  Also, this needs to be aware of the sign; we don't want to
>> sign-extend an unsigned short.
>
> I tried many different examples and couldn't come up with one that would
> fail. Do you have a concrete example? :)

For the crash, something like the following (which doesn't actually
work because clang doesn't support TF mode yet):
typedef int ty128 __attribute__((__mode__(TF)));
long x = ((int*)0)[(ty128)1 << 100] - ((int*)0);

For the incorrect output, try something like the following with "clang
-emit-llvm -triple x86_64-unknown-linux-gnu":
long y = &((int*)0)[0xFFFFFFFFu] - ((int*)0);

>>> +      const QualType Type = E->getLHS()->getType();
>>> +      const QualType ElementType =
>>> Type->getAsPointerType()->getPointeeType();
>>> +
>>> +      uint64_t D = LHSValue.getLValueOffset() -
>>> RHSValue.getLValueOffset();
>>> +      D /= Info.Ctx.getTypeSize(ElementType) / 8;
>>
>> The result here isn't necessarily positive; an unsigned divide will
>> give an incorrect result in such cases.
>>
>
> Same here. I couldn't reproduce the error with an example.

Try something like the following with "clang -emit-llvm -triple
x86_64-unknown-linux-gnu":

long y = ((int*)0) - &((int*)0)[1];

-Eli



More information about the cfe-commits mailing list