![]() Apparently, there is overflow (as defined above) and C-overflow (as used by the standard). “A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type”Ġ. If you want to find out how C (and C++) handles integer overflow, you have to take a look at chapter 6.7.5 “Types”, the following sentence in particular: Other possibilities exist, ranging from saturation (the overflowing value is set to the largest/smallest value and stays there), to raising an exception, to doing whatever an implementation fancies. Wrap-around is, however, only one way to handle integer overflow. If you add one to the largest value, you arrive at the smallest if you subtract one from the smallest value, you get the largest. Wrap-around denotes that an integer type behaves like a circle that is, it has no beginning and no end. One possibility is what is conventionally referred to as wrap-around. Now that we know what overflow is, we can tackle the question what happens on overflow. And so shall I, for the rest of this discussion. It’s common among programmers to use the term overflow for both, overrun and underrun of a type’s value range. And, see you in the following articles.Īgain, you can find all related source code at □. We hope you have learned something interesting. In this article, you have discovered the integer overflow vulnerability in the smart contract, how an attacker exploits it, and the solutions to fix the issue. Please refer to this link for the latest OpenZeppelin’s SafeMath library. The library used in the code is just a simplified version for brevity’s sake. The contract applies the SafeMath library to prevent any underflow or overflow issues in lines 46, 50, 56, and 58. The FixedMoonToken contract above is the fixed version of the InsecureMoonToken. Therefore, you can buy or sell 1, 2, 3, or 46 tokens but not 33.5.Įxactly, the InsecureMoonToken contract is vulnerable to integer overflow. In other words, 1 MOON token will always have a fixed value of 1 Ether. The MOON is a non-divisible token (token decimals = 0) pegged with 1 Ether. The following presents the InsecureMoonToken contract that allows a user to buy or sell MOON tokens. Nonetheless, feel free to contact Valix Consulting for your smart contract consulting and auditing services. Hence, do not use the source code in this article in your production. Some contracts are vulnerable, some are simplified for minimal, some contain malicious code. The smart contracts in this article are used to demonstrate vulnerability issues only. You can find all related source code at □. This article will explore how the integer overflow happens and how to prevent it when developing your smart contracts. Integer overflow might be one of the most commonly known issues in the smart contract security field. For this reason, we are proud to present this series of articles regarding Solidity smart contract security to educate and improve the knowledge in this domain to the public. ![]() Smart contract security is one of the biggest impediments toward the mass adoption of the blockchain. Originally published in Valix Consulting’s Medium.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |