LWN.net Logo

What Every C Programmer Should Know About Undefined Behavior #3/3

What Every C Programmer Should Know About Undefined Behavior #3/3

Posted May 22, 2011 0:29 UTC (Sun) by jd (guest, #26381)
Parent article: What Every C Programmer Should Know About Undefined Behavior #3/3

Unsafe optimizations aught to be a solvable problem. Once the executable code has been generated, it should be possible to say in a well-defined subset of cases whether the resultant binary is trying to perform a nonsense operation.

If the code has symbols that state what line(s) of source are involved and what optimization(s) were used, you could always give a really verbose warning to say that this specific combination is potentially invalid.

Or, for cleverer results, make the compilation multi-pass. Where a block of intermediate source produces bad results, disable the most likely offending optimizations and try again until either there's nothing left or the tests say that the compilation looks ok.

The disadvantage of the former is that the warnings would be horribly verbose and drown developers in information. The disadvanatge of the latter is that it's slow and of indeterminate duration for relatively marginal gains in what you hope are fringe cases (where the original code is good but the compiler's herustics screw up).

In general, fixing the developers is easier than fixing the freakier cases.


(Log in to post comments)

What Every C Programmer Should Know About Undefined Behavior #3/3

Posted May 22, 2011 1:52 UTC (Sun) by welinder (guest, #4699) [Link]

> Once the executable code has been generated, it should be possible to
> say in a well-defined subset of cases whether the resultant binary is
> trying to perform a nonsense operation.

That would be a solution to the halting problem. So, no -- not possible.

What Every C Programmer Should Know About Undefined Behavior #3/3

Posted May 22, 2011 8:36 UTC (Sun) by nteon (subscriber, #53899) [Link]

well, patches welcome of course :)

What Every C Programmer Should Know About Undefined Behavior #3/3

Posted May 23, 2011 15:19 UTC (Mon) by HelloWorld (guest, #56129) [Link]

> That would be a solution to the halting problem.
How so?

What Every C Programmer Should Know About Undefined Behavior #3/3

Posted May 23, 2011 15:53 UTC (Mon) by vonbrand (subscriber, #4458) [Link]

It is possible to find a set of conditions in which nothing strange happens (by simulating the program running in each case, or some sophisticated variation thereof). The problem is that doing so is a lot of work, and/or the resulting set of "safe" uses is disappointingly small.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds