{"id":14612,"date":"2016-05-23T10:32:23","date_gmt":"2016-05-23T14:32:23","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=14612"},"modified":"2016-05-23T10:34:45","modified_gmt":"2016-05-23T14:34:45","slug":"c-compilers-break-for-objects-larger-than-ptrdiff_max-bytes","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/05\/23\/c-compilers-break-for-objects-larger-than-ptrdiff_max-bytes\/","title":{"rendered":"C Compilers Break for Objects Larger Than PTRDIFF_MAX Bytes"},"content":{"rendered":"<p><a href=\"http:\/\/trust-in-soft.com\/objects-larger-than-ptrdiff_max-bytes\/\">Pascal Cuoq<\/a> (via <a href=\"https:\/\/twitter.com\/jfroy\/status\/733682208834215938\">Jean-Francois Roy<\/a>):<\/p>\n<blockquote cite=\"http:\/\/trust-in-soft.com\/objects-larger-than-ptrdiff_max-bytes\/\"><p>The <code>malloc<\/code> call in the program below succeeds (the program would return 0 if the malloc invocation failed), despite Clang having the same half-the-address-space limitation for 64-bit as for 32-bit.<\/p>\n<p>[&#8230;]<\/p>\n<p>Unfortunately, an optimizing compiler&rsquo;s code appears to be littered with optimizations that assume object sizes are less than <code>PTRDIFF_MAX<\/code>. As an example, we all know that X86 Clang and GCC implement pointer comparison as an <a href=\"https:\/\/godbolt.org\/g\/gfLSQX\">assembly unsigned comparison<\/a> (&ldquo;cmpl&rdquo; is an agnostic comparison instruction, but here it is followed by &ldquo;cmoval&rdquo;, where the &ldquo;a&rdquo;, for &ldquo;above&rdquo;, indicates conditional move based on an unsigned comparison). This means that it is allowed to compare pointers inside a putative 2.5GiB array on a 32-bit platform, right?<\/p>\n<p>Not all pointer comparisons.<\/p>\n<p>Because of <a href=\"https:\/\/godbolt.org\/g\/R8DFWW\">this optimization<\/a>.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Pascal Cuoq (via Jean-Francois Roy): The malloc call in the program below succeeds (the program would return 0 if the malloc invocation failed), despite Clang having the same half-the-address-space limitation for 64-bit as for 32-bit. [&#8230;] Unfortunately, an optimizing compiler&rsquo;s code appears to be littered with optimizations that assume object sizes are less than PTRDIFF_MAX. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[131,45,230,255,285,46,138,71],"class_list":["post-14612","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bug","tag-c","tag-clang","tag-compiler","tag-gcc","tag-languagedesign","tag-optimization","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=14612"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14612\/revisions"}],"predecessor-version":[{"id":14613,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14612\/revisions\/14613"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=14612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=14612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=14612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}