[cfe-commits] PATCH: Enhance array bounds checking

David Blaikie dblaikie at gmail.com
Thu Jul 21 13:44:01 PDT 2011


>> void foo(int n) {
>>   char x[5];
>>   if (n > 0) bar(x + 6 - n);
>> }
>
> Strictly speaking, that isn't a false positive... I can't think of how
> we would actually produce anything other than the expected result,

[I'm not sure how closely you were considering your knowledge of the
implementation of clang (which is no doubt greater than my own)
compared to things a C++ compiler could possibly do, even if clang
doesn't do them today.]

Actually it's not so hard to imagine a case just like this where this
UB could result in some interesting results. It wouldn't be hard for
the compiler to prove that x + 6 is out of range, therefor the
condition (n > 0) must always be false, so it could just remove the
code entirely. Chris gave some similar examples in his blog series on
UB.

- David




More information about the cfe-commits mailing list