{"id":14883,"date":"2010-02-19T07:00:00","date_gmt":"2010-02-19T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/02\/19\/the-normal-string-manipulation-functions-stop-on-a-null-terminator-so-be-careful-when-manipulating-double-null-terminated-strings\/"},"modified":"2010-02-19T07:00:00","modified_gmt":"2010-02-19T07:00:00","slug":"the-normal-string-manipulation-functions-stop-on-a-null-terminator-so-be-careful-when-manipulating-double-null-terminated-strings","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100219-00\/?p=14883","title":{"rendered":"The normal string manipulation functions stop on a null terminator, so be careful when manipulating double-null-terminated strings"},"content":{"rendered":"<p>\nOne of the many gotchas of working with\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/10\/08\/9904646.aspx\">\ndouble-null-terminated strings<\/a>\nis accidentally using functions on them which were designed\nto operate on single-null-terminated strings.\nNow, you do need to use those single-null-terminated strings,\nbut you also need to know when they won&#8217;t do what you want.\n<\/p>\n<p>\nOne of the responses to my psychic detection that somebody\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/10\/08\/9904646.aspx\">\npassed a single-null-terminated string to\n<code>SHFileOperation<\/code><\/a>\nis,\n&#8220;Oh, no, I double-null-terminated it. Look:\n<\/p>\n<pre>\nsprintf(szDeletePath, \"%s\\0\", file_to_delete);\n<\/pre>\n<p>\nSee, I put an extra <code>\\0<\/code> at the end.&#8221;\n<\/p>\n<p>\nWell, yeah, you put an extra <code>\\0<\/code> at the end, but\nall that does is terminate the format string.\nThe <code>sprintf<\/code> function accepts its format string\nas a traditional null-terminated string.\nWhen it sees the <code>\\0<\/code> you stuck into the string,\nit thinks it found the end of the string.\nIt can&#8217;t read your mind and say,\n&#8220;Oh, this null is not a terminator.\nIt&#8217;s an embedded null.&#8221;\n<\/p>\n<p>\nA simple mistake, but something that needs to be called out.\nTo be fair, most people recognize this mistake as soon as it&#8217;s\npointed out to them.\nYou just have to remember to point it out to them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the many gotchas of working with double-null-terminated strings is accidentally using functions on them which were designed to operate on single-null-terminated strings. Now, you do need to use those single-null-terminated strings, but you also need to know when they won&#8217;t do what you want. One of the responses to my psychic detection 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-14883","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>One of the many gotchas of working with double-null-terminated strings is accidentally using functions on them which were designed to operate on single-null-terminated strings. Now, you do need to use those single-null-terminated strings, but you also need to know when they won&#8217;t do what you want. One of the responses to my psychic detection that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/14883","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=14883"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/14883\/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=14883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=14883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=14883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}