{"id":53,"date":"2007-04-12T06:18:00","date_gmt":"2007-04-11T22:18:00","guid":{"rendered":"http:\/\/www.freemindworld.com\/blog\/2007\/04\/0412_%e5%b0%8f%e5%87%bd%e6%95%b0%ef%bc%8c%e5%a4%a7%e9%97%ae%e9%a2%98-2.shtml"},"modified":"2012-01-30T14:19:10","modified_gmt":"2012-01-30T06:19:10","slug":"070411_cpp_string_functions","status":"publish","type":"post","link":"https:\/\/www.freemindworld.com\/blog\/2007\/070411_cpp_string_functions.shtml","title":{"rendered":"\u5c0f\u51fd\u6570\uff0c\u5927\u95ee\u9898"},"content":{"rendered":"<p>\u5bf9\u4e8estrcpy\uff0csprintf\uff0cstrcat\u8fd9\u4e9b\u4e0d\u5b89\u5168\u7684\u51fd\u6570\u7684\u8ba8\u8bba\u5e94\u8be5\u7b97\u662f\u4e0d\u5c11\u4e86\uff0c\u5927\u5bb6\u90fd\u77e5\u9053\u53ef\u4ee5\u7528strncpy\uff0csnprintf\u548cstrncat\u6765\u66ff\u6362\u5b83\u4eec\u3002\u4f46\u5b9e\u9645\u5de5\u4f5c\u4e2d\u4f3c\u4e4e\u8fd9\u4e9b\u201c\u5b89\u5168\u7248\u201d\u7684\u51fd\u6570\u5e26\u6765\u7684\u7591\u60d1\u8fd8\u662f\u4e0d\u5c0f\uff0c\u603b\u7ed3\u4e86\u4e00\u4e0b\uff0c\u5217\u5728\u8fd9\u91cc\u3002<\/p>\n<p>1.sprintf(char * str, const char * format, &#8230;) -&gt; snprintf(char * str, size_t size, const char * format, &#8230;)<\/p>\n<p>vsprintf(char * str, const char * format, va_list ap) -&gt; vsnprintf(char * str, size_t size, const char * format, va_list ap)<\/p>\n<p>\u6309\u7167C\/C++\u6807\u51c6\uff0csnprintf\u548cvsnprintf\u6c38\u8fdc\u4e0d\u4f1a\u5f80str\u4e2d\u8f93\u51fa\u591a\u4e8esize\u6240\u6307\u5b9a\u7684\u5b57\u8282\u6570\u7684\u5b57\u7b26\uff08\u5305\u62ec\u7ed3\u5c3e\u7684&#8217;\/0&#8217;\uff09\uff0c\u5b83\u4eec\u4e5f\u4fdd\u8bc1\u4e86\u4e00\u5b9a\u4f1a\u5728str\u4e2d\u5199\u5165&#8217;\/0&#8217;\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u540e\u4e0d\u7528\u62c5\u5fc3\u7ed3\u5c3e\u7684\u95ee\u9898\u3002<\/p>\n<p>\u4e3e\u4f8b:<\/p>\n<p><code>char buf[5];<br \/>\nsnprintf(buf, 5, \"This is a test string.\"); \/\/ buf becomes \"This\", buf[4] is '\/0'<br \/>\nsnprintf(buf, 6, \"This is a test string.\"); \/\/ ERROR: buffer overflow<br \/>\nsnprintf(buf, 5, \"abc\"); \/\/ buf becomes \"abc\", the value of buf[3] is '\/0', buf[4] is undefined.<\/code><\/p>\n<p>\u7136\u800c\uff0cVC\u4e2d\u7684_snprintf\u51fd\u6570\u5e76\u6ca1\u6709\u6309\u7167\u8fd9\u6837\u7684\u89c4\u5b9a\u6765\u505a\uff0c\u5b83\u5728\u8f93\u51fa\u7f13\u51b2\u533a\u4e0d\u591f\u5927\u65f6\u5c31\u4e0d\u4f1a\u8f93\u51fa\u7ed3\u5c3e\u7684&#8217;\/0&#8242;(\u8ddfstrncpy\u7684\u884c\u4e3a\u7c7b\u4f3c)\u3002\u6240\u4ee5\u8981\u8ba9\u4e0a\u9762\u7684\u7a0b\u5e8f\u5de5\u4f5c\u6b63\u5e38\uff0c\u5fc5\u987b\u505a\u76f8\u5e94\u7684\u4fee\u6539\u3002<\/p>\n<p><code>char buf[5];<br \/>\n_snprintf(buf, 5, \"This is a test string.\");\/\/ buf becomes \"This \", buf[4] is ' '<br \/>\nbuf[4] = 0; \/\/ buf[4] is '\/0' now.<br \/>\n_snprintf(buf, 6, \"This is a test string.\"); \/\/ ERROR: buffer overflow<br \/>\n_snprintf(buf, 5, \"abc\"); \/\/ buf becomes \"abc\", the value of buf[3] is '\/0', buf[4] is undefined.<\/code><\/p>\n<p>\u5982\u679c\u8981\u4fdd\u8bc1\u53ef\u79fb\u690d\u6027\uff0c\u5c31\u5f97\u6309VC\u7684\u5199\u6cd5\uff0c\u505a\u4e00\u6b21\u5bf9\u4e8e\u6807\u51c6\u6765\u8bf4\u5f88\u591a\u4f59\u7684\u201c\u586b0\u201d\u7684\u64cd\u4f5c\uff0c\u518d\u901a\u8fc7\u5b9a\u4e49\u5b8f\u6765\u533a\u522b\u5e73\u53f0\u5206\u522b\u9009\u7528snprintf\u6216_snprintf\u3002<\/p>\n<p>2. strcat(char * dest, const char * src) -&gt; strncat(char * dest, const char * src, size_t n);<\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u6bd4\u8f83\u7b80\u5355\uff0c\u5b83\u4fdd\u8bc1\u4e0d\u4f1a\u5199\u5165\u591a\u4e8en+1\u4e2a\u5b57\u7b26\uff0c\u5e76\u4e14\u4fdd\u8bc1\u6700\u540e\u4e00\u5b9a\u4ee5&#8217;\/0&#8217;\u7ed3\u675f\u3002<\/p>\n<p>\u4e3e\u4f8b\uff1a<\/p>\n<p><code>char dest[5] = \"abc\";<br \/>\nstrncat(dest, \"defghijklmn\", 5 - 3 - 1); \/\/ dest becomes \"abcd\", dest[4] is '\/0',<br \/>\n                                         \/\/ always minus the buffer length by 1 as the value of n.<br \/>\nstrncat(dest, \"defghijklmn\", 5 - 3);     \/\/ ERROR: buffer overflow<\/code><\/p>\n<p>3. strcpy(char * dest, const char * src) -&gt; strncpy(char * dest, const char * src, size_t n);<\/p>\n<p>strncpy\u662f\u4e00\u4e2a\u6bd4\u8f83\u5bb9\u6613\u51fa\u9519\u7684\u51fd\u6570\uff0c\u5b83\u4fdd\u8bc1\u590d\u5236src\u4e2d\u4e0d\u591a\u4e8en\u5b57\u8282\u7684\u5185\u5bb9\uff0c\u4f46\u662f\u5982\u679csrc\u7684\u524dn\u4e2a\u5b57\u8282\u4e2d\u6ca1\u6709\u5305\u542b&#8217;\/0&#8217;\uff0c\u5c31\u4f1a\u5bfc\u81f4 dest\u6ca1\u6709\u6b63\u5e38\u7684\u4ee5&#8217;\/0&#8217;\u7ec8\u6b62\u3002\u53e6\u5916\uff0c\u5b83\u8fd8\u4fdd\u8bc1\u5982\u679csrc\u7684\u957f\u5ea6\u5c0f\u4e8en\uff0c\u5219dest\u5269\u4f59\u7684\u90e8\u5206\u90fd\u4f1a\u4ee5&#8217;\/0&#8217;\u586b\u5145\u3002\u5728\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u6709\u4e00\u4e2a\u6700\u4f73\u5b9e\u8df5\u5c31 \u662f\u5148\u628adest\u4ee50\u586b\u5145\uff0c\u5e76\u4f20\u5165dest\u7684\u957f\u5ea6\u51cf1\u7684\u503c\u4f5c\u4e3an\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5b89\u5168\u3002<\/p>\n<p>\u4e3e\u4f8b\uff1a<\/p>\n<p><code>\u3000\u3000char buf[5] = {0}; \/\/ Always zero-fill the buffer,<br \/>\n                             \/\/ always use this form to initialize the stack arrays to get<br \/>\n                             \/\/better performance over memset.<br \/>\nchar * buf2 = new char[5];<br \/>\nmemset(buf2, 0, 5);          \/\/ Always zero-fill the buffer<br \/>\nstrncpy(buf, \"abcde\", 5);    \/\/ ERROR: buf is not null-terminated.<br \/>\nstrncpy(buf2, \"abcde\", 5 - 1); \/\/ Right, always minus the buffer length by 1<br \/>\n                               \/\/as the value of n. buf becomes \"abcd\", buf[4]<br \/>\n                               \/\/is '\/0' by initilization;<br \/>\n                               \/\/ This is a common error in the existing code.<br \/>\nchar buf3[5];<br \/>\nconst char * str = \"Test\";<br \/>\nstrncpy(buf3, str, strlen(str)); \/\/ Wrong, buf may not null-terminated,<br \/>\n                                 \/\/ potential buffer-overflow<br \/>\nstrncpy(buf3, str, strlen(str)+1); \/\/ Wrong, potential buffer-overflow.<br \/>\n                                   \/\/No difference with using strcpy.<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u4e8estrcpy\uff0csprintf\uff0cstrcat\u8fd9\u4e9b\u4e0d\u5b89\u5168\u7684\u51fd\u6570\u7684\u8ba8\u8bba\u5e94\u8be5\u7b97\u662f\u4e0d\u5c11 &hellip; <a href=\"https:\/\/www.freemindworld.com\/blog\/2007\/070411_cpp_string_functions.shtml\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-11"],"_links":{"self":[{"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":0,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freemindworld.com\/blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}