{"id":13343,"date":"2010-07-23T07:00:00","date_gmt":"2010-07-23T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/07\/23\/if-im-not-supposed-to-call-isbadxxxptr-how-can-i-check-if-a-pointer-is-bad\/"},"modified":"2010-07-23T07:00:00","modified_gmt":"2010-07-23T07:00:00","slug":"if-im-not-supposed-to-call-isbadxxxptr-how-can-i-check-if-a-pointer-is-bad","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100723-00\/?p=13343","title":{"rendered":"If I&#039;m not supposed to call IsBadXxxPtr, how can I check if a pointer is bad?"},"content":{"rendered":"<p>\nSome time ago, I opined that\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/09\/27\/773741.aspx\">\n<code>Is&shy;Bad&shy;Xxx&shy;Ptr<\/code>\nshould really be called <code>Crash&shy;Program&shy;Randomly<\/code><\/a>\nand you really should just let the program crash if somebody passes\nyou a bad pointer.\nIt is common to put pointer validation code at the start of functions\nfor debugging purposes\n(as long as you don&#8217;t make logic decisions based on whether the pointer\nis valid).\nBut if you can&#8217;t use <code>Is&shy;Bad&shy;Xxx&shy;Ptr<\/code>,\nhow can you validate\nthe pointer?\n<\/p>\n<p>\nWell, to validate a write pointer, write to it.\nTo validate a read pointer, read from it.\nIf the pointer is invalid, you&#8217;ll crash, and at a predictable\nlocation, before the function has gotten halfway through its\nprocessing (making post-mortem debugging more difficult).\nHere are the functions I used:\n<\/p>\n<pre>\n\/\/ Make sure to disable compiler optimizations in these functions\n\/\/ so the code won't be removed by the optimizer.\nvoid DebugValidateWritePtr(void *p, size_t cb)\n{\n memcpy(p, p, cb);\n}\nvoid DebugValidateReadPtr(void *p, size_t cb)\n{\n memcmp(p, p, cb);\n}\n<\/pre>\n<p>\nTo verify that a buffer can be written to, we write to it\nby copying it to itself.\nSimilarly, to verify that a buffer can be read, we read from it\nby comparing it to itself.\nThe result of the operation is not important; we are interested\nin the side-effect of the memory access itself.\n<\/p>\n<p>\nNote that the <code>Debug&shy;Validate&shy;Write&shy;Ptr<\/code> function\nis not thread-safe: If another thread modifies the buffer while\nwe are copying it to itself, the write may be lost.\nBut code that does this violates one of the\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/03\/20\/555511.aspx\">\nground rules for programming<\/a> (specifically the parameter\nstability requirements).\nOf course, if your function has specific behavior requirements\nbeyond the ground rules, then that helper function may not\nwork for you.\nI&#8217;m just putting it out there as a courtesy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago, I opined that Is&shy;Bad&shy;Xxx&shy;Ptr should really be called Crash&shy;Program&shy;Randomly and you really should just let the program crash if somebody passes you a bad pointer. It is common to put pointer validation code at the start of functions for debugging purposes (as long as you don&#8217;t make logic decisions based on whether [&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-13343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Some time ago, I opined that Is&shy;Bad&shy;Xxx&shy;Ptr should really be called Crash&shy;Program&shy;Randomly and you really should just let the program crash if somebody passes you a bad pointer. It is common to put pointer validation code at the start of functions for debugging purposes (as long as you don&#8217;t make logic decisions based on whether [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/13343","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=13343"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/13343\/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=13343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=13343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=13343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}