[cfe-dev] Summer of Code idea -- detecting undefined behavior (fwd)

Ahmed Charles ahmedcharles at gmail.com
Wed Mar 24 15:06:40 PDT 2010


On Tue, Mar 23, 2010 at 10:20 PM, Chris Hacking
<chacking at cs.washington.edu> wrote:
> I suspect some of these tests might incur a significant performance penalty
> if run as part of compilation. Simple and easy tests are good, but anything
> that is too expensive might make more sense to move into the Clang Static
> Analyzer instead.
>
> That said, some work from the CSA might be usable here. For example, I
> believe that the CSA already detects divide-by-zero. While a full analysis
> of the source code would be overkill for the compiler to do every time,
> adding the ability to detect divide-by-zero from literals:
>
> int a = 5; int b = 5/0;
>
> or using constant propagation:
>
> int a = 5; int b = 0; int c = a/b;
>
> would certainly be useful and probably relatively easy.
>
> Chris Hacking

Assuming the static analyzer doesn't actually produce executables (why
would it, it's static), it wouldn't be useful here, since finding
undefined behavior in general, is a runtime thing, like:

int i;
cin >> i;
int x = 5 / i;

static analysis can't detect this case of divide by zero. It could
tell that i is runtime defined and not proven to be zero, therefore it
should be tested for zero before used in the denominator of a divide.

That said, you could eliminate checks for cases where it's proven that
undefined behavior could not occur, based on static analysis, which
would result in the resulting program running faster.



More information about the cfe-dev mailing list