{"id":2589,"date":"2024-08-14T00:18:00","date_gmt":"2024-08-13T16:18:00","guid":{"rendered":"https:\/\/systw.net\/note\/?p=2589"},"modified":"2025-08-14T12:05:41","modified_gmt":"2025-08-14T04:05:41","slug":"sc-integer-overflow-and-underflow","status":"publish","type":"post","link":"https:\/\/systw.net\/note\/archives\/2589","title":{"rendered":"SC Integer Overflow and Underflow"},"content":{"rendered":"\n<p>\u6574\u6578\u6ea2\u4f4d\uff08Overflow\uff09\u8207\u4e0b\u6ea2\uff08Underflow\uff09\u662f\u667a\u80fd\u5408\u7d04\u4e2d\u7684\u4e00\u7a2e\u5b89\u5168\u6f0f\u6d1e\uff0c\u6e90\u65bc\u4ee5\u592a\u574a\u865b\u64ec\u6a5f\uff08EVM\uff09\u4e2d\u6574\u6578\u8cc7\u6599\u578b\u5225\u7684\u56fa\u5b9a\u5927\u5c0f\u9650\u5236\u3002EVM \u4f7f\u7528\u56fa\u5b9a\u9577\u5ea6\u7684\u6574\u6578\u578b\u5225\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>uint8<\/strong>\uff08\u7121\u7b26\u865f 8 \u4f4d\u6574\u6578\uff09\uff1a\u53ea\u80fd\u8868\u793a 0 \u5230 255 \u7684\u6578\u5b57\u3002<\/li>\n\n\n\n<li><strong>int8<\/strong>\uff08\u6709\u7b26\u865f 8 \u4f4d\u6574\u6578\uff09\uff1a\u53ef\u8868\u793a -128 \u5230 127 \u7684\u6578\u5b57\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7576\u7b97\u8853\u904b\u7b97\u7d50\u679c\u8d85\u51fa\u578b\u5225\u7684\u7bc4\u570d\u6642\uff0c\u6703\u767c\u751f\u6ea2\u4f4d\u6216\u4e0b\u6ea2\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6ea2\u4f4d\uff08Overflow\uff09<\/strong>\uff1a\u8a66\u5716\u5c07\u503c\u8a2d\u70ba\u8d85\u51fa\u578b\u5225\u6700\u5927\u7bc4\u570d\u3002\u4f8b\u5982\uff0c\u5c07 <code>uint8<\/code> \u7684\u503c 255 \u52a0 1\uff0c\u7d50\u679c\u4e0d\u662f 256\uff0c\u800c\u662f\u300c\u56de\u7e5e\u300d\u5230 0\u3002<\/li>\n\n\n\n<li><strong>\u4e0b\u6ea2\uff08Underflow\uff09<\/strong>\uff1a\u8a66\u5716\u5c07\u503c\u6e1b\u5230\u5c0f\u65bc\u578b\u5225\u6700\u5c0f\u7bc4\u570d\u3002\u4f8b\u5982\uff0c\u5c07 <code>uint8<\/code> \u7684\u503c 0 \u6e1b 1\uff0c\u7d50\u679c\u4e0d\u662f -1\uff0c\u800c\u662f\u300c\u56de\u7e5e\u300d\u5230 255\u3002<\/li>\n\n\n\n<li><strong>\u6709\u7b26\u865f\u6574\u6578<\/strong>\uff1a\u5c0d\u65bc <code>int8<\/code>\uff0c\u5982\u679c\u5f9e -128 \u6e1b 1\uff0c\u7d50\u679c\u6703\u56de\u7e5e\u5230 127\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u9019\u7a2e\u884c\u70ba\u985e\u4f3c\u65bc\u6c7d\u8eca\u91cc\u7a0b\u8868\uff08\u9054\u5230 999999 \u5f8c\u91cd\u7f6e\u70ba 000000\uff09\u6216\u9031\u671f\u6027\u6578\u5b78\u51fd\u6578\uff08\u5982\u6b63\u5f26\u51fd\u6578\uff09\u3002<\/p>\n\n\n\n<p><strong>\u91cd\u8981\u6ce8\u610f\u4e8b\u9805<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 Solidity <strong>0.8.0 \u53ca\u4ee5\u4e0a\u7248\u672c<\/strong>\uff0c\u7de8\u8b6f\u5668\u5167\u5efa\u4e86\u6ea2\u4f4d\u548c\u4e0b\u6ea2\u6aa2\u67e5\uff0c\u7576\u767c\u751f\u6ea2\u4f4d\u6216\u4e0b\u6ea2\u6642\uff0c\u4ea4\u6613\u6703\u81ea\u52d5\u56de\u6efe\u3002<\/li>\n\n\n\n<li>Solidity 0.8.0 \u5f15\u5165\u4e86 <code>unchecked<\/code> \u95dc\u9375\u5b57\uff0c\u5141\u8a31\u958b\u767c\u8005\u5728\u7279\u5b9a\u60c5\u6cc1\u4e0b\u7981\u7528\u9019\u4e9b\u6aa2\u67e5\u4ee5\u7bc0\u7701 gas\uff08\u4f8b\u5982\uff0c\u7576\u78ba\u5b9a\u4e0d\u6703\u767c\u751f\u6ea2\u4f4d\u6642\uff09\u3002\u9019\u6703\u8b93\u7b97\u8853\u904b\u884c\u70ba\u56de\u6b78\u5230\u820a\u7248\u672c\u7684\u884c\u70ba\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u7bc4\u4f8b\uff08\u5b58\u5728\u6f0f\u6d1e\u7684\u5408\u7d04\uff09<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u6613\u53d7\u6574\u6578\u6ea2\u4f4d\u548c\u4e0b\u6ea2\u653b\u64ca\u7684\u5408\u7d04\u7bc4\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.4.17;\n\ncontract Solidity_OverflowUnderflow {\n    uint8 public balance;\n\n    constructor() public {\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">balance = 255; \/\/ Maximum value of uint8<\/mark>\n    }\n\n    \/\/ Increments the balance by a given value\n    function increment(uint8 value) public {\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">balance += value; \/\/ Vulnerable to overflow<\/mark>\n    }\n\n    \/\/ Decrements the balance by a given value\n    function decrement(uint8 value) public {\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">balance -= value; \/\/ Vulnerable to underflow<\/mark>\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6f0f\u6d1e\u5206\u6790<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6ea2\u4f4d\u6f0f\u6d1e<\/strong>\uff1a\u5047\u8a2d <code>balance = 255<\/code>\uff0c\u8abf\u7528 <code>increment(1)<\/code> \u6703\u5c0e\u81f4 <code>255 + 1 = 0<\/code>\uff08\u6ea2\u4f4d\uff09\uff0c\u56e0\u70ba <code>uint8<\/code> \u6700\u5927\u503c\u70ba 255\uff0c\u8d85\u904e\u5f8c\u56de\u7e5e\u5230 0\u3002<\/li>\n\n\n\n<li><strong>\u4e0b\u6ea2\u6f0f\u6d1e<\/strong>\uff1a\u5047\u8a2d <code>balance = 0<\/code>\uff0c\u8abf\u7528 <code>decrement(1)<\/code> \u6703\u5c0e\u81f4 <code>0 - 1 = 255<\/code>\uff08\u4e0b\u6ea2\uff09\u3002<\/li>\n\n\n\n<li><strong>\u653b\u64ca\u60c5\u5883<\/strong>\uff1a<\/li>\n\n\n\n<li>\u653b\u64ca\u8005\u53ef\u8abf\u7528 <code>increment(1)<\/code> \u5c07 <code>balance<\/code> \u5f9e 255 \u8b8a\u70ba 0\uff0c\u9020\u6210\u9918\u984d\u932f\u8aa4\u91cd\u7f6e\u3002<\/li>\n\n\n\n<li>\u653b\u64ca\u8005\u53ef\u8abf\u7528 <code>decrement(1)<\/code> \u5f9e 0 \u8b8a\u70ba 255\uff0c\u88fd\u9020\u5927\u91cf\u865b\u5047\u9918\u984d\uff0c\u9032\u800c\u63d0\u9818\u4e0d\u5c6c\u65bc\u81ea\u5df1\u7684\u8cc7\u91d1\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5f71\u97ff<\/h4>\n\n\n\n<p>\u6574\u6578\u6ea2\u4f4d\u548c\u4e0b\u6ea2\u53ef\u80fd\u5c0e\u81f4\u4ee5\u4e0b\u56b4\u91cd\u5f8c\u679c\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8cc7\u91d1\u640d\u5931<\/strong>\uff1a\u653b\u64ca\u8005\u53ef\u901a\u904e\u6ea2\u4f4d\u6216\u4e0b\u6ea2\u64cd\u7e31\u9918\u984d\uff0c\u63d0\u53d6\u8d85\u51fa\u5176\u5408\u6cd5\u64c1\u6709\u7684\u8cc7\u91d1\u3002<\/li>\n\n\n\n<li><strong>\u908f\u8f2f\u7834\u58de<\/strong>\uff1a\u932f\u8aa4\u7684\u6578\u503c\u53ef\u80fd\u6539\u8b8a\u5408\u7d04\u7684\u9810\u671f\u884c\u70ba\uff0c\u4f8b\u5982\u5141\u8a31\u672a\u6388\u6b0a\u7684\u64cd\u4f5c\u6216\u751f\u6210\u904e\u91cf\u4ee3\u5e63\u3002<\/li>\n\n\n\n<li><strong>\u5354\u8b70\u7834\u58de<\/strong>\uff1a\u9019\u4e9b\u6f0f\u6d1e\u53ef\u80fd\u5c0e\u81f4\u5354\u8b70\u529f\u80fd\u5931\u5e38\uff0c\u5f71\u97ff\u7528\u6236\u4fe1\u4efb\u548c\u751f\u614b\u7cfb\u7d71\u7a69\u5b9a\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fee\u5fa9\u65b9\u6cd5<\/h4>\n\n\n\n<p>\u70ba\u9632\u6b62\u6574\u6578\u6ea2\u4f4d\u548c\u4e0b\u6ea2\uff0c\u958b\u767c\u8005\u61c9\u63a1\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n\n\n\n<p><strong>\u4f7f\u7528 Solidity 0.8.0 \u6216\u66f4\u9ad8\u7248\u672c<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f9e Solidity 0.8.0 \u958b\u59cb\uff0c\u7de8\u8b6f\u5668\u81ea\u52d5\u6aa2\u67e5\u6ea2\u4f4d\u548c\u4e0b\u6ea2\uff0c\u4ea4\u6613\u6703\u5728\u767c\u751f\u7570\u5e38\u6642\u56de\u6efe\u3002<\/li>\n\n\n\n<li>\u9019\u662f\u6700\u7c21\u55ae\u7684\u4fee\u5fa9\u65b9\u6cd5\uff0c\u7121\u9700\u984d\u5916\u7a0b\u5f0f\u78bc\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4f7f\u7528\u5b89\u5168\u6578\u5b78\u5eab<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c0d\u65bc\u4f4e\u65bc 0.8.0 \u7684\u7248\u672c\uff0c\u53ef\u4f7f\u7528 OpenZeppelin \u7684 <strong>SafeMath<\/strong> \u5eab\uff0c\u8a72\u5eab\u63d0\u4f9b\u5b89\u5168\u7684\u7b97\u8853\u904b\u7b97\u51fd\u6578\uff08\u5982 <code>add()<\/code>\u3001<code>sub()<\/code>\uff09\uff0c\u5728\u6ea2\u4f4d\u6216\u4e0b\u6ea2\u6642\u81ea\u52d5\u56de\u6efe\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u624b\u52d5\u6aa2\u67e5<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u95dc\u9375\u904b\u7b97\u524d\u624b\u52d5\u6aa2\u67e5\u8f38\u5165\u503c\uff0c\u78ba\u4fdd\u7d50\u679c\u4e0d\u6703\u8d85\u51fa\u7bc4\u570d\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u4fee\u5fa9\u5f8c\u7684\u7bc4\u4f8b\u5408\u7d04<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4fee\u5fa9\u5f8c\u7684\u5408\u7d04\uff0c\u4f7f\u7528 Solidity 0.8.0 \u7684\u5167\u5efa\u6ea2\u4f4d\u6aa2\u67e5\uff0c\u4e26\u52a0\u5165\u984d\u5916\u9632\u8b77\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ SPDX-License-Identifier: MIT\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">pragma solidity ^0.8.0;<\/mark>\n\ncontract Solidity_OverflowUnderflow {\n    uint8 public balance;\n\n    constructor() {\n        balance = 255; \/\/ Maximum value of uint8\n    }\n\n    \/\/ Increments the balance by a given value\n    function increment(uint8 value) public {\n        balance += value; \/\/ Solidity 0.8.x automatically checks for overflow\n    }\n\n    \/\/ Decrements the balance by a given value\n    function decrement(uint8 value) public {\n        <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">require(balance &gt;= value, \"Underflow detected\"); <\/mark> \n        balance -= value;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u4fee\u5fa9\u5167\u5bb9<\/strong>\uff1a<\/p>\n\n\n\n<p><strong>Solidity 0.8.0 \u6ea2\u4f4d\u6aa2\u67e5<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 <code>increment<\/code> \u4e2d\uff0c<code>balance += value<\/code> \u82e5\u5c0e\u81f4\u6ea2\u4f4d\uff08\u4f8b\u5982\uff0c255 + 1\uff09\uff0c\u7de8\u8b6f\u5668\u6703\u81ea\u52d5\u56de\u6efe\u4ea4\u6613\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u624b\u52d5\u4e0b\u6ea2\u6aa2\u67e5<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 <code>decrement<\/code> \u4e2d\uff0c\u65b0\u589e <code>require(balance &gt;= value)<\/code>\uff0c\u78ba\u4fdd\u4e0d\u6703\u767c\u751f\u4e0b\u6ea2\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u7e3d\u7d50<\/h4>\n\n\n\n<p>\u6574\u6578\u6ea2\u4f4d\u548c\u4e0b\u6ea2\u662f\u667a\u80fd\u5408\u7d04\u4e2d\u7684\u5371\u96aa\u6f0f\u6d1e\uff0c\u53ef\u80fd\u5c0e\u81f4\u9918\u984d\u64cd\u7e31\u3001\u672a\u6388\u6b0a\u64cd\u4f5c\u548c\u8cc7\u91d1\u640d\u5931\u3002\u9019\u4e9b\u554f\u984c\u6e90\u65bc EVM \u6574\u6578\u578b\u5225\u7684\u56fa\u5b9a\u7bc4\u570d\u9650\u5236\uff0c\u5c0e\u81f4\u6578\u503c\u56de\u7e5e\u884c\u70ba\u3002Solidity 0.8.0 \u53ca\u4ee5\u4e0a\u7248\u672c\u7684\u5167\u5efa\u6ea2\u4f4d\u6aa2\u67e5\u662f\u6700\u7c21\u55ae\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u7d50\u5408 OpenZeppelin \u7684 SafeMath \u5eab\u6216\u624b\u52d5\u6aa2\u67e5\u53ef\u9032\u4e00\u6b65\u589e\u5f37\u5b89\u5168\u6027\u3002\u4fee\u5fa9\u5f8c\u7684\u5408\u7d04\u5c55\u793a\u4e86\u5982\u4f55\u5229\u7528\u73fe\u4ee3 Solidity \u529f\u80fd\u548c\u984d\u5916\u6aa2\u67e5\u4f86\u9632\u6b62\u9019\u4e9b\u6f0f\u6d1e\uff0c\u78ba\u4fdd\u5408\u7d04\u7684\u53ef\u9760\u6027\u548c\u5b89\u5168\u6027\u3002\u958b\u767c\u8005\u5728\u8a2d\u8a08\u5408\u7d04\u6642\u61c9\u59cb\u7d42\u4f7f\u7528\u6700\u65b0\u7248\u672c\u4e26\u9032\u884c\u5168\u9762\u6e2c\u8a66\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6574\u6578\u6ea2\u4f4d\uff08Overflow\uff09\u8207\u4e0b\u6ea2\uff08Underflow\uff09\u662f\u667a &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[371],"tags":[],"class_list":["post-2589","post","type-post","status-publish","format-standard","hentry","category-web3-security"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2589","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/comments?post=2589"}],"version-history":[{"count":2,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2589\/revisions"}],"predecessor-version":[{"id":2612,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/2589\/revisions\/2612"}],"wp:attachment":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/media?parent=2589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/categories?post=2589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/tags?post=2589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}