{"id":3452,"date":"2025-04-06T00:10:22","date_gmt":"2025-04-05T16:10:22","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3452"},"modified":"2025-04-06T00:10:22","modified_gmt":"2025-04-05T16:10:22","slug":"c-%e8%af%ad%e8%a8%80%e6%9c%aa%e5%ae%9a%e4%b9%89%e8%a1%8c%e4%b8%ba%ef%bc%88undefined-behavior%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/06\/c-%e8%af%ad%e8%a8%80%e6%9c%aa%e5%ae%9a%e4%b9%89%e8%a1%8c%e4%b8%ba%ef%bc%88undefined-behavior%ef%bc%89\/","title":{"rendered":"C \u8bed\u8a00\u672a\u5b9a\u4e49\u884c\u4e3a\uff08Undefined Behavior\uff09"},"content":{"rendered":"\n<p>C \u8bed\u8a00\u4e2d\u7684 <strong>\u672a\u5b9a\u4e49\u884c\u4e3a\uff08Undefined Behavior\uff0c\u7b80\u79f0 UB\uff09<\/strong> \u662f C \u6807\u51c6\u4e2d\u6700\u91cd\u8981\u5374\u4e5f\u6700\u5371\u9669\u7684\u6982\u5ff5\u4e4b\u4e00\u3002\u5b83\u610f\u5473\u7740\u7a0b\u5e8f\u5728\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u65f6\uff0c<strong>\u7f16\u8bd1\u5668\u53ef\u4ee5\u4efb\u610f\u5904\u7406\u8be5\u64cd\u4f5c\u7684\u540e\u679c<\/strong>\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3001\u8f93\u51fa\u9519\u8bef\u7ed3\u679c\uff0c\u751a\u81f3\u5728\u4e0d\u540c\u7f16\u8bd1\u5668\/\u5e73\u53f0\u4e0b\u8868\u73b0\u5b8c\u5168\u4e0d\u540c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4ec0\u4e48\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\uff1f<\/h2>\n\n\n\n<p>\u5728 C \u8bed\u8a00\u6807\u51c6\u4e2d\uff0c\u67d0\u4e9b\u884c\u4e3a\u662f <strong>\u672a\u5b9a\u4e49\u7684\uff08undefined\uff09<\/strong>\uff0c\u610f\u6307\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u6807\u51c6\u672a\u6307\u5b9a\u8fd9\u4e9b\u884c\u4e3a\u7684\u7ed3\u679c\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u7531\u5904\u7406\u3002<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>\u26a0\ufe0f \u4e00\u65e6\u89e6\u53d1 UB\uff0c\u7a0b\u5e8f\u5c31\u5904\u4e8e\u201c\u6cd5\u5f8b\u4e4b\u5916\u201d\u72b6\u6001\uff0c\u7f16\u8bd1\u5668 <strong>\u53ef\u80fd\u4f18\u5316\u6389\u4f60\u4ee5\u4e3a\u6709\u7528\u7684\u4ee3\u7801<\/strong>\uff0c\u751f\u6210\u4f60\u5b8c\u5168\u65e0\u6cd5\u9884\u6d4b\u7684\u7ed3\u679c\uff01<\/p>\n\n\n\n<p>\ud83d\udcd6 \u6765\u81ea C \u6807\u51c6\u7684\u5b9a\u4e49\uff08ISO\/IEC 9899:2018 C18\uff09\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>undefined behavior<\/strong>: behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this document imposes no requirements.<\/p>\n<\/blockquote>\n\n\n\n<p>\ud83d\udd17 \u5b98\u65b9\u94fe\u63a5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/en.cppreference.com\/w\/c\/language\/behavior\">https:\/\/en.cppreference.com\/w\/c\/language\/behavior<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5e38\u89c1\u7684\u672a\u5b9a\u4e49\u884c\u4e3a\u7c7b\u578b<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u5e38\u89c1\u4e14\u5371\u9669\u7684 UB \u60c5\u51b5\uff08\u6bcf\u4e2a\u90fd\u9644\u6709\u793a\u4f8b\uff09\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u8bbf\u95ee\u672a\u521d\u59cb\u5316\u53d8\u91cf<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int x;\nprintf(\"%d\\n\", x); \/\/ UB\uff1ax \u6ca1\u6709\u521d\u59cb\u5316<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u9664\u4ee5\u96f6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int x = 10, y = 0;\nint z = x \/ y;  \/\/ UB\uff1a\u6574\u6570\u9664\u4ee5\u96f6<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u6570\u7ec4\u8d8a\u754c\u8bbf\u95ee<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int a&#91;3] = {1, 2, 3};\na&#91;3] = 10;  \/\/ UB\uff1a\u6709\u6548\u7d22\u5f15\u662f a&#91;0] \u5230 a&#91;2]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6307\u9488\u60ac\u6302 \/ \u4f7f\u7528\u91ca\u653e\u7684\u5185\u5b58<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int *p = malloc(sizeof(int));\nfree(p);\n*p = 5;  \/\/ UB\uff1a\u8bbf\u95ee\u5df2\u91ca\u653e\u7684\u5185\u5b58<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u53d8\u66f4 const \u5bf9\u8c61\u7684\u503c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const int a = 10;\nint *p = (int *)&amp;a;\n*p = 20;  \/\/ UB\uff1a\u5c1d\u8bd5\u4fee\u6539 const \u6570\u636e<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u591a\u6b21\u4fee\u6539\u4e00\u4e2a\u53d8\u91cf\u4e14\u6ca1\u6709\u987a\u5e8f\u89c4\u5219\uff08\u987a\u5e8f\u70b9\u4e4b\u524d\u91cd\u590d\u4fee\u6539\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int i = 1;\ni = i++ + ++i;  \/\/ UB\uff1ai \u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u88ab\u591a\u6b21\u4fee\u6539\uff0c\u987a\u5e8f\u672a\u5b9a\u4e49<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u7a7a\u6307\u9488\u89e3\u5f15\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int *p = NULL;\n*p = 5;  \/\/ UB\uff1a\u7a7a\u6307\u9488\u4e0d\u80fd\u89e3\u5f15\u7528<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. \u6307\u9488\u8fd0\u7b97\u8d8a\u51fa\u6570\u7ec4\u8fb9\u754c\uff08\u5373\u4f7f\u4e0d\u8bbf\u95ee\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int a&#91;5];\nint *p = &amp;a&#91;5];  \/\/ UB\uff1aC \u6807\u51c6\u4ec5\u5141\u8bb8\u6307\u5411\u672b\u5c3e\u5143\u7d20\u4e4b\u540e\u7684\u4e00\u4e2a\u5730\u5740<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9. \u4fee\u6539\u5b57\u7b26\u4e32\u5e38\u91cf\uff08\u53ea\u8bfb\u6570\u636e\u6bb5\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>char *str = \"hello\";\nstr&#91;0] = 'H';  \/\/ UB\uff1a\u4fee\u6539\u5b57\u7b26\u4e32\u5e38\u91cf<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u4e3a\u4ec0\u4e48 UB \u662f\u5fc5\u8981\u7684\uff1f<\/h2>\n\n\n\n<p>\u5c3d\u7ba1 UB \u542c\u8d77\u6765\u5f88\u6050\u6016\uff0c\u4f46\u5b83\u5728\u6027\u80fd\u548c\u5e73\u53f0\u517c\u5bb9\u6027\u4e0a\u662f<strong>\u975e\u5e38\u91cd\u8981\u7684\u59a5\u534f<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4f18\u52bf<\/th><th>\u89e3\u91ca<\/th><\/tr><\/thead><tbody><tr><td>\ud83d\udd25 \u6027\u80fd\u4f18\u5316<\/td><td>\u7f16\u8bd1\u5668\u53ef\u5ffd\u7565\u65e0\u610f\u4e49\u7684\u4ee3\u7801\uff0c\u751f\u6210\u66f4\u5feb\u7684\u673a\u5668\u7801<\/td><\/tr><tr><td>\ud83d\ude80 \u8de8\u5e73\u53f0\u517c\u5bb9<\/td><td>\u67d0\u4e9b\u884c\u4e3a\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u6839\u672c\u65e0\u4e00\u81f4\u5b9e\u73b0<\/td><\/tr><tr><td>\ud83d\udcac \u63d0\u793a\u7a0b\u5e8f\u5458\u5199\u51fa\u66f4\u5b89\u5168\u3001\u53ef\u63a7\u7684\u4ee3\u7801<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f8b\u5982\uff1a\u4e0b\u9762\u7684\u4ee3\u7801\u5728\u5f00\u542f\u4f18\u5316\uff08\u5982 <code>-O2<\/code>\uff09\u65f6\u7f16\u8bd1\u5668\u53ef\u80fd\u5220\u9664\u6574\u4e2a <code>if<\/code> \u5206\u652f\uff01<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int *p = NULL;\nif (p) {\n    *p = 5;\n}<\/code><\/pre>\n\n\n\n<p>\u7f16\u8bd1\u5668\u53ef\u8ba4\u4e3a <code>p == NULL<\/code> \u6c38\u8fdc\u4e3a\u5047\uff0c\u4f18\u5316\u6389 if \u5206\u652f\uff08\u751a\u81f3\u4e0d\u63d0\u793a UB\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u5982\u4f55\u68c0\u6d4b\u548c\u907f\u514d UB\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u6700\u4f73\u5b9e\u8df5<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u5de5\u5177\/\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\ud83e\uddea \u4f7f\u7528\u9759\u6001\u5206\u6790\u5de5\u5177<\/td><td>Clang Static Analyzer, cppcheck<\/td><\/tr><tr><td>\ud83d\udee0\ufe0f \u5f00\u542f\u7f16\u8bd1\u5668\u8b66\u544a<\/td><td><code>-Wall -Wextra -Wpedantic<\/code><\/td><\/tr><tr><td>\ud83d\udd0d \u4f7f\u7528\u5185\u5b58\u68c0\u6d4b\u5de5\u5177<\/td><td><a href=\"https:\/\/valgrind.org\/\">Valgrind<\/a>\u3001<a href=\"https:\/\/clang.llvm.org\/docs\/AddressSanitizer.html\">ASan\uff08AddressSanitizer\uff09<\/a><\/td><\/tr><tr><td>\ud83d\udce6 \u5c3d\u91cf\u4f7f\u7528\u521d\u59cb\u5316\u53d8\u91cf<\/td><td>\u5c24\u5176\u662f\u7ed3\u6784\u4f53\u3001\u6570\u7ec4<\/td><\/tr><tr><td>\ud83d\udcda \u907f\u514d\u5371\u9669\u4ee3\u7801\u6a21\u5f0f<\/td><td>\u591a\u6b21\u8d4b\u503c\u3001\u88f8\u6307\u9488\u64cd\u4f5c\u3001\u7c7b\u578b\u8f6c\u6362<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u6743\u5a01\u53c2\u8003\u8d44\u6599\u4e0e\u51fa\u7ad9\u94fe\u63a5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6765\u6e90<\/th><th>\u94fe\u63a5<\/th><\/tr><\/thead><tbody><tr><td>C \u6807\u51c6\u8349\u6848\uff08N1570 PDF\uff09<\/td><td><a href=\"https:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n1570.pdf\">https:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n1570.pdf<\/a><\/td><\/tr><tr><td>cppreference \u5bf9 UB \u7684\u89e3\u91ca<\/td><td><a href=\"https:\/\/en.cppreference.com\/w\/c\/language\/behavior\">https:\/\/en.cppreference.com\/w\/c\/language\/behavior<\/a><\/td><\/tr><tr><td>ISO\/IEC C18 \u6807\u51c6\u6458\u5f55\u8bf4\u660e<\/td><td><a href=\"https:\/\/www.iso.org\/standard\/74528.html\">https:\/\/www.iso.org\/standard\/74528.html<\/a><\/td><\/tr><tr><td>Clang Undefined Behavior Sanitizer<\/td><td><a href=\"https:\/\/clang.llvm.org\/docs\/UndefinedBehaviorSanitizer.html\">https:\/\/clang.llvm.org\/docs\/UndefinedBehaviorSanitizer.html<\/a><\/td><\/tr><tr><td>GCC -fsanitize=undefined<\/td><td><a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Instrumentation-Options.html\">https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Instrumentation-Options.html<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>UB \u662f C \u8bed\u8a00\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206<\/strong>\uff0c\u8d4b\u4e88\u4e86\u7f16\u8bd1\u5668\u5f3a\u5927\u4f18\u5316\u80fd\u529b\uff0c\u4f46\u4e5f\u8981\u6c42\u5f00\u53d1\u8005\u6709\u66f4\u9ad8\u7684\u8d23\u4efb\u5fc3\u3002<\/li>\n\n\n\n<li><strong>\u4f60\u65e0\u6cd5\u4f9d\u8d56 UB \u8868\u73b0\u7684\u4e00\u81f4\u6027<\/strong>\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u89e6\u53d1 UB\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u5de5\u5177\u68c0\u6d4b UB \u662f\u4e13\u4e1a\u5f00\u53d1\u5fc5\u5907\u624b\u6bb5<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C \u8bed\u8a00\u4e2d\u7684 \u672a\u5b9a\u4e49\u884c\u4e3a\uff08Undefined Behavior\uff0c\u7b80\u79f0 UB\uff09 \u662f C \u6807\u51c6\u4e2d\u6700\u91cd\u8981\u5374\u4e5f\u6700\u5371\u9669\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3453,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-3452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3452"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3452\/revisions"}],"predecessor-version":[{"id":3454,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3452\/revisions\/3454"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3453"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}