{"id":47794,"date":"2024-12-02T17:53:41","date_gmt":"2024-12-02T09:53:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47794\/"},"modified":"2024-12-02T17:53:41","modified_gmt":"2024-12-02T09:53:41","slug":"ph%e5%87%bd%e6%95%b0%e5%ae%89%e5%85%a8%e9%97%ae%e9%a2%98%e6%a3%80%e6%b5%8b%e8%af%84%e4%bc%b0%e4%b9%8b%e9%81%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47794\/","title":{"rendered":"ph\u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>ph\u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aph\u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729936528671cbc9095e70.jpg\" class=\"aligncenter\" title=\"ph\u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053\u63d2\u56fe\" alt=\"ph\u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053\u63d2\u56fe\" \/><\/p>\n<p><strong>pH \u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u51fd\u6570\u5b89\u5168\u5bf9\u4e8e\u6d89\u53ca\u5b89\u5168\u5173\u952e\u7cfb\u7edf\u7684\u5d4c\u5165\u5f0f\u8f6f\u4ef6\u81f3\u5173\u91cd\u8981\u3002pH \u51fd\u6570\u8c03\u7528\u673a\u5236\u662f\u8fd9\u4e9b\u7cfb\u7edf\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u7684\u51fd\u6570\u5b89\u5168\u673a\u5236\uff0c\u53ef\u4ee5\u9694\u79bb\u6545\u969c\u529f\u80fd\u5e76\u786e\u4fdd\u7cfb\u7edf\u5b89\u5168\u8fd0\u884c\u3002\u4f46\u662f\uff0cpH \u51fd\u6570\u53ef\u80fd\u5b58\u5728\u5b89\u5168\u6f0f\u6d1e\uff0c\u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u6545\u969c\u3002\u56e0\u6b64\uff0c\u81f3\u5173\u91cd\u8981\u7684\u662f\u68c0\u6d4b\u548c\u8bc4\u4f30\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<p><strong>\u68c0\u6d4b\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u9759\u6001\u5206\u6790<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u5de5\u5177\u5206\u6790\u6e90\u4ee3\u7801\u4ee5\u8bc6\u522b\u6f5c\u5728\u7684\u5b89\u5168\u6f0f\u6d1e\u3002<\/li>\n<li>\u68c0\u6d4b\u6570\u636e\u7c7b\u578b\u9519\u8bef\u3001\u7a7a\u6307\u9488\u5f15\u7528\u548c\u7ade\u4e89\u6761\u4ef6\u3002<\/li>\n<\/ul>\n<p><strong>\u52a8\u6001\u6d4b\u8bd5<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u4e13\u95e8\u7684\u6d4b\u8bd5\u6846\u67b6\u5728\u4eff\u771f\u6216\u771f\u5b9e\u73af\u5883\u4e2d\u6267\u884c\u4ee3\u7801\u3002<\/li>\n<li>\u89e6\u53d1\u8fb9\u754c\u6761\u4ef6\u548c\u9519\u8bef\u573a\u666f\u4ee5\u53d1\u73b0\u96be\u4ee5\u901a\u8fc7\u9759\u6001\u5206\u6790\u68c0\u6d4b\u5230\u7684\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u6570\u636e\u7c7b\u578b\u9519\u8bef<\/strong><\/p>\n<pre>void unsafeFunction(int x) {\n  \/\/ \u5e94\u8be5\u4f7f\u7528 uint8_t \u7c7b\u578b\uff0c\u4f46\u9519\u8bef\u5730\u4f7f\u7528\u4e86 int\n  \/\/ \u5f53 x \u5927\u4e8e 255 \u65f6\uff0c\u4f1a\u5bfc\u81f4\u6ea2\u51fa\n  uint8_t y = (uint8_t)x;\n}<\/pre>\n<p><strong>\u52a8\u6001\u6d4b\u8bd5<\/strong><\/p>\n<pre>TEST(UnsafeFunctionTest, DataTypeOverflow) {\n  \/\/ \u4f20\u9012\u4e00\u4e2a\u8d85\u51fa uint8_t \u8303\u56f4\u7684\u503c\n  ASSERT_DEATH(unsafeFunction(256), \".*\");\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u91c7\u7528\u9759\u6001\u5206\u6790\u548c\u52a8\u6001\u6d4b\u8bd5\u76f8\u7ed3\u5408\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u68c0\u6d4b\u548c\u8bc4\u4f30 pH \u51fd\u6570\u4e2d\u7684\u5b89\u5168\u95ee\u9898\u3002\u8fd9\u5bf9\u4e8e\u786e\u4fdd\u5d4c\u5165\u5f0f\u8f6f\u4ef6\u5728\u5b89\u5168\u5173\u952e\u7cfb\u7edf\u4e2d\u7684\u5b89\u5168\u8fd0\u884c\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aph\u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u5b89\u5168\u95ee\u9898,PH\u51fd\u6570\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ph\u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053 \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300aph\u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 pH \u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053 \u5f15\u8a00 \u51fd\u6570\u5b89\u5168\u5bf9\u4e8e\u6d89\u53ca\u5b89\u5168\u5173\u952e\u7cfb\u7edf\u7684\u5d4c\u5165\u5f0f\u8f6f\u4ef6\u81f3\u5173\u91cd\u8981\u3002pH \u51fd\u6570\u8c03\u7528\u673a\u5236\u662f\u8fd9\u4e9b\u7cfb\u7edf\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u7684\u51fd\u6570\u5b89\u5168\u673a\u5236\uff0c\u53ef\u4ee5\u9694\u79bb\u6545\u969c\u529f\u80fd\u5e76\u786e\u4fdd\u7cfb\u7edf\u5b89\u5168\u8fd0\u884c\u3002\u4f46\u662f\uff0cpH \u51fd\u6570\u53ef\u80fd\u5b58\u5728\u5b89\u5168\u6f0f\u6d1e\uff0c\u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u6545\u969c\u3002\u56e0\u6b64\uff0c\u81f3\u5173\u91cd\u8981\u7684\u662f\u68c0\u6d4b\u548c\u8bc4\u4f30\u8fd9\u4e9b\u95ee\u9898\u3002 \u68c0\u6d4b\u65b9\u6cd5 \u9759\u6001\u5206\u6790 \u4f7f\u7528\u5de5\u5177\u5206\u6790\u6e90\u4ee3\u7801\u4ee5\u8bc6\u522b\u6f5c\u5728\u7684\u5b89\u5168\u6f0f\u6d1e\u3002 \u68c0\u6d4b\u6570\u636e\u7c7b\u578b\u9519\u8bef\u3001\u7a7a\u6307\u9488\u5f15\u7528\u548c\u7ade\u4e89\u6761\u4ef6\u3002 \u52a8\u6001\u6d4b\u8bd5 \u4f7f\u7528\u4e13\u95e8\u7684\u6d4b\u8bd5\u6846\u67b6\u5728\u4eff\u771f\u6216\u771f\u5b9e\u73af\u5883\u4e2d\u6267\u884c\u4ee3\u7801\u3002 \u89e6\u53d1\u8fb9\u754c\u6761\u4ef6\u548c\u9519\u8bef\u573a\u666f\u4ee5\u53d1\u73b0\u96be\u4ee5\u901a\u8fc7\u9759\u6001\u5206\u6790\u68c0\u6d4b\u5230\u7684\u95ee\u9898\u3002 \u5b9e\u6218\u6848\u4f8b \u6570\u636e\u7c7b\u578b\u9519\u8bef void unsafeFunction(int x) { \/\/ \u5e94\u8be5\u4f7f\u7528 uint8_t \u7c7b\u578b\uff0c\u4f46\u9519\u8bef\u5730\u4f7f\u7528\u4e86 int \/\/ \u5f53 x \u5927\u4e8e 255 \u65f6\uff0c\u4f1a\u5bfc\u81f4\u6ea2\u51fa uint8_t y = (uint8_t)x; } \u52a8\u6001\u6d4b\u8bd5 TEST(UnsafeFunctionTest, DataTypeOverflow) { \/\/ \u4f20\u9012\u4e00\u4e2a\u8d85\u51fa uint8_t \u8303\u56f4\u7684\u503c ASSERT_DEATH(unsafeFunction(256), &#8220;.*&#8221;); } \u7ed3\u8bba \u901a\u8fc7\u91c7\u7528\u9759\u6001\u5206\u6790\u548c\u52a8\u6001\u6d4b\u8bd5\u76f8\u7ed3\u5408\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u68c0\u6d4b\u548c\u8bc4\u4f30 pH \u51fd\u6570\u4e2d\u7684\u5b89\u5168\u95ee\u9898\u3002\u8fd9\u5bf9\u4e8e\u786e\u4fdd\u5d4c\u5165\u5f0f\u8f6f\u4ef6\u5728\u5b89\u5168\u5173\u952e\u7cfb\u7edf\u4e2d\u7684\u5b89\u5168\u8fd0\u884c\u81f3\u5173\u91cd\u8981\u3002 \u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aph\u51fd\u6570\u5b89\u5168\u95ee\u9898\u68c0\u6d4b\u8bc4\u4f30\u4e4b\u9053\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u5b89\u5168\u95ee\u9898,PH\u51fd\u6570\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47794","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47794"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47794\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}