{"id":98775,"date":"2018-05-17T07:00:00","date_gmt":"2018-05-17T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=98775"},"modified":"2019-03-13T00:44:04","modified_gmt":"2019-03-13T07:44:04","slug":"20180517-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180517-00\/?p=98775","title":{"rendered":"How do I create a SAL annotation for a structure with a variable-length array?"},"content":{"rendered":"<p>Some Windows structures <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20040826-00\/?p=38043\">end with an array of size 1<\/a>. If you try to access any members of that array beyond the first, you may get a static analysis error. <\/p>\n<pre>\ntypedef struct THINGGROUP\n{\n  DWORD NumberOfThings;\n  THING Things[ANYSIZE_ARRAY];\n};\n\nvoid ProcessAllTheThings(_In_ const THINGGROUP* group)\n{\n for (DWORD index = 0; index &lt; group-&gt;NumberOfThings; index++) {\n  \/\/ static analysis warning: possible index past end of array\n  \/\/ when NumberOfThings &gt;= 2\n  ProcessOneThing(group-&gt;Things[index]);\n }\n}\n<\/pre>\n<p>How do you tell the Visual Studio static analysis tool that the size of the <code>Things<\/code> array is specified by the <code>NumberOfThings<\/code> member? <\/p>\n<p>You use <a HREF=\"https:\/\/docs.microsoft.com\/en-us\/visualstudio\/code-quality\/annotating-structs-and-classes\">the <code>_Field_size_<\/code> annotation<\/a>. The documentation doesn&#8217;t really give an example of this case, so here you go: <\/p>\n<pre>\ntypedef struct THINGGROUP\n{\n  DWORD NumberOfThings;\n  <font COLOR=\"blue\">_Field_size_(NumberOfThings)<\/font>\n  THING Things[ANYSIZE_ARRAY];\n};\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The <CODE>_Field_size_<\/CODE> annotation does the trick.<\/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-98775","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The <CODE>_Field_size_<\/CODE> annotation does the trick.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98775","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=98775"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98775\/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=98775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=98775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=98775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}