<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">On Wed, 21 Nov 2018 16:45</div>From: Nemo wrote:<div class=""><br class=""></div><div class="">…</div><div class=""><blockquote type="cite" class="">You did not give even one example of what you mean, so I will pick a<br class="">typical one: Integer overflow.<br class=""><br class="">What should happen if the mathematical sum of two positive signed<br class="">integers does not fit in an int? (BTW, how many bits should that be,<br class="">exactly?) If you care about correctness -- and thus security -- there<br class="">are only three possible answers:<br class=""><br class=""> 1) Don't do that.<br class=""> 2) An exception should be raised.<br class=""> 3) The width of the result should grow to hold the sum.<br class=""><br class="">There are approximately zero cases where "produce a negative result" is<br class="">what the programmer actually wants.<br class=""><br class="">The C/C++ language spec goes with option (1). If you want (2) or (3),<br class="">you can use a library to get them (see Microsoft's SafeInt and<br class="">Boost.Multiprecision, respectively).<br class=""><br class="">You will find the same is true for all forms of undefined behavior:<br class="">Defining it in a sane way would not be performant, and defining it in a<br class="">performant way would not be sane. Thus "don't do that or use a library".</blockquote></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">We had a lengthy discussion on this list in April 2014 regaring assert statement removal, <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475" class="">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475</a> , which is one example where "produce a negative result” is what the programmer sanely wants. They are trying to enforce “don’t do that.” Yes there are other ways to do that, but how much performance would be lost if compilers never removed an assert? </div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">The interaction invariably goes like this:<br class="">Inexperienced C or C++ programmer is surprised by some optimization and<br class="">petitions compiler authors that said optimization should be<br class="">disallowed.</blockquote><br class=""></div><div class="">It’s not necessarily a question of disallowing some optimizations. It’s an engineering management/configuration control problem as much as a compiler issue. There seems to be compiler options for GCC that avoid the more egregious behaviors.  The problem is who selects those options and when, and how does a medium to large project control that? If a software module that has been designed and tested under one set of optimization options is later incorporated into a system that is recompiled with different options, how does QA or security management know? One solution might be a way for an object module to limit what optimizations are used or a library method that returns at run time the compiler options used to compile it. If the limits on optimizations could be incorporated in the module’s source code, then at least any changes in optimization level would receive the same attention and review as a change in the program logic itself. </div><div class=""><br class=""></div><div class="">Do such facilities exist in any of the major tool chains?</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">Or use a language that takes a performance hit everywhere to give<br class="">defined behavior everywhere. There are plenty of them</blockquote></div><div class=""><br class=""></div><div class=""><div class="">The suggestion to use a different language ignores the enormous amount of C an C++ code already in use. It’s economically infeasible to rewrite it all. If the compiler writer and computer security communities could have a reasonable dialog on this, continued use of C and C++ code, including code written and maintained by less than top-notch programmers, could be made significantly safer. </div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Arnold Reinhold</div><div class=""><br class=""></div><div class=""><br class=""></div></div></body></html>