{"id":21753,"date":"2008-07-03T10:00:00","date_gmt":"2008-07-03T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/07\/03\/how-did-the-invalid-floating-point-operand-exception-get-raised-when-i-disabled-it\/"},"modified":"2008-07-03T10:00:00","modified_gmt":"2008-07-03T10:00:00","slug":"how-did-the-invalid-floating-point-operand-exception-get-raised-when-i-disabled-it","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080703-00\/?p=21753","title":{"rendered":"How did the invalid floating point operand exception get raised when I disabled it?"},"content":{"rendered":"<p>Last time, we learned about <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/07\/02\/8679191.aspx\"> the dangers of uninitialized floating point variables<\/a> but left with a puzzle: Why wasn&#8217;t this caught during internal testing?<\/p>\n<p> I dropped a hint when I described how <code>SNaN<\/code>s work: You have to ask the processor to raise an exception when it encounters a signaling NaN, and the program disabled that exception. Why was an exception being raised when it had been disabled? <\/p>\n<p> The clue to the cause was that the customer that was encountering the crash reported that it tended to happen after they printed a report. It turns out that <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/07\/02\/8679191.aspx#8681705\"> the customer&#8217;s printer driver<\/a> was re-enabling the <i>invalid operand<\/i> exception in its <code>DLL_PROCESS_ATTACH<\/code> handler. Since the exception was enabled, the <code>SNaN<\/code> exception, which was previously masked, was now live, and it crashed the program. <\/p>\n<p> I&#8217;ve also seen DLLs change the floating point rounding state in their <code>DLL_PROCESS_ATTACH<\/code> handler. This behavior can be traced back to <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/07\/02\/8679191.aspx#8681805\"> old versions of the C runtime library<\/a> which reset the floating point state as part of their <code>DLL_PROCESS_ATTACH<\/code>; this behavior was corrected as long ago as 2002 (possibly even earlier; I don&#8217;t know for sure). Obviously that printer driver was even older. Good luck convincing the vendor to fix a bug in a driver for a printer they most likely don&#8217;t even manufacture any more. If anything, they&#8217;ll probably just treat it <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2003\/12\/19\/44644.aspx\"> as incentive for you to buy a new printer<\/a>. <\/p>\n<p> When you load external code into your process, you implicitly trust that the code won&#8217;t screw you up. This is just another example of how a DLL can inadvertently screw you up. <\/p>\n<p> <b>Sidebar<\/b> <\/p>\n<p> One might argue that the <code>LoadLibrary<\/code> function should save the floating point state before loading a library and restore it afterwards. This is an easy suggestion to make in retrospect. Writing software would be so much easier if people would just extend the courtesy of coming up with a comprehensive list of &#8220;bugs applications will have that you should protect against&#8221; <i>before<\/i> you design the platform. That way, when a new class of application bugs is found, and they say &#8220;You should&#8217;ve protected against this!&#8221;, you can point to the list and say, &#8220;Nuh, uh, you didn&#8217;t put it on the list. You had your chance.&#8221; <\/p>\n<p> As a mental exercise for yourself: Come up with a list of &#8220;all the bugs that the <code>LoadLibrary<\/code> function should protect against&#8221; and <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/01\/24\/7213752.aspx\"> how the <code>LoadLibrary<\/code> function would go about doing it<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last time, we learned about the dangers of uninitialized floating point variables but left with a puzzle: Why wasn&#8217;t this caught during internal testing? I dropped a hint when I described how SNaNs work: You have to ask the processor to raise an exception when it encounters a signaling NaN, and the program disabled that [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-21753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Last time, we learned about the dangers of uninitialized floating point variables but left with a puzzle: Why wasn&#8217;t this caught during internal testing? I dropped a hint when I described how SNaNs work: You have to ask the processor to raise an exception when it encounters a signaling NaN, and the program disabled that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/21753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=21753"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/21753\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=21753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=21753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=21753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}