{"id":9523,"date":"2011-09-29T07:00:00","date_gmt":"2011-09-29T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/09\/29\/appearing-to-succeed-is-a-valid-form-of-undefined-behavior-but-its-still-undefined\/"},"modified":"2011-09-29T07:00:00","modified_gmt":"2011-09-29T07:00:00","slug":"appearing-to-succeed-is-a-valid-form-of-undefined-behavior-but-its-still-undefined","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110929-00\/?p=9523","title":{"rendered":"Appearing to succeed is a valid form of undefined behavior, but it&#039;s still undefined"},"content":{"rendered":"<p>\nA customer requested a clarification on\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/aa366701.aspx\">\nthe MSDN documentation\nfor the <code>HeapFree<\/code> function<\/a>.\n<\/p>\n<blockquote CLASS=\"q\"><p>\nThe MSDN documentation says that if the <code>lpMem<\/code>\nparameter is <code>NULL<\/code>, then the behavior is undefined.\nIs this true?\n<\/p><\/blockquote>\n<p>\nAs explicitly stated in MSDN, the behavior is undefined.\nObserve that\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/aa383701.aspx\">\nthe annotation<\/a>\non the <code>lpMem<\/code>\nparameter is <code>__in<\/code>,\nwhich means that the parameter must be a non-<code>NULL<\/code>\nvalue provided by the caller.\n(If <code>NULL<\/code> were permitted, the annotation would have\nbeen <code>__in_opt<\/code>.)\n<\/p>\n<p>\nUndefined behavior means that <i>anything can happen<\/i>.\nThe program might crash immediately.\nIt might crash five minutes later.\nIt might send email to your boss saying that you screwed up\nand then read you Vogon poetry.\nOr maybe not.\n<\/p>\n<p>\nMSDN says don&#8217;t do it, so don&#8217;t do it.\n<\/p>\n<p>\nThe customer explained why they were interested in knowing\nmore information about undefined behavior:\n<\/p>\n<blockquote CLASS=\"q\"><p>\nWe were interested because there is a mismatch between\nthe semantics of a function we are implementing\n(where <code>NULL<\/code> is valid and ignored)\nand the function <code>HeapFree<\/code> we are using\nas the implementation.\nIt looks like Windows Vista returns <code>TRUE<\/code> if\nyou pass <code>NULL<\/code>.\n<\/p><\/blockquote>\n<p>\nIf there is a mismatch in semantics\nbetween the function you are implementing\nand the function you are calling,\nit is your responsibility as the programmer to bridge\nthe gap.\nThe customer didn&#8217;t say what function they were implementing,\nbut I&#8217;m guessing it was something like\n<code>operator delete<\/code>.\nSince your function accepts <code>NULL<\/code> but\n<code>HeapFree<\/code> doesn&#8217;t,\nit is your responsibility to filter out <code>NULL<\/code> parameters.\n<\/p>\n<pre>\nvoid operator delete(void* ptr) throw ()\n{\n <font COLOR=\"blue\">if (ptr != NULL)<\/font>\n  HeapFree(CustomHeap, 0, ptr);\n}\n<\/pre>\n<p>\nThis concept goes by the fancy name of\n<a HREF=\"http:\/\/en.wikipedia.org\/wiki\/Adapter_pattern\">\nthe Adapter Pattern<\/a>.\nThe less fancy name is\n<a HREF=\"http:\/\/en.wikipedia.org\/wiki\/Wrapper_function\">\nwrapper function<\/a>.\n<\/p>\n<p>\nAnd the value returned by <code>HeapFree<\/code> on Windows Vista\nis irrelevant.\nPretending to succeed is a valid form of undefined behavior,\nbecause\n<i>anything<\/i> qualifies as undefined behavior.\n<\/p>\n<p>\n(Of course,\nyou can&#8217;t assume that returning <code>TRUE<\/code> will always be\nthe result of triggering undefined behavior.\nAfter all, if you could rely on it, then it wouldn&#8217;t be undefined any\nmore!)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer requested a clarification on the MSDN documentation for the HeapFree function. The MSDN documentation says that if the lpMem parameter is NULL, then the behavior is undefined. Is this true? As explicitly stated in MSDN, the behavior is undefined. Observe that the annotation on the lpMem parameter is __in, which means that the [&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-9523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer requested a clarification on the MSDN documentation for the HeapFree function. The MSDN documentation says that if the lpMem parameter is NULL, then the behavior is undefined. Is this true? As explicitly stated in MSDN, the behavior is undefined. Observe that the annotation on the lpMem parameter is __in, which means that the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/9523","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=9523"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/9523\/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=9523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=9523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=9523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}