자주나오는패턴

[Common Vulnerabilities] Integer Overflow and Underflow

0xperiagoge 2024. 1. 10. 21:08

컴퓨터 프로그래밍에서 overflow/underflow는 산술 연산이 타입의 최대 또는 최소 크기에 도달할 때 발생한다. 예를 들어, 숫자가 uint8 타입에 저장되면, 이 숫자는 8비트 부호 없는 숫자로 0에서 2^8-1 범위에 저장된다. 컴퓨터 프로그래밍에서 정수 오버플로우는 산술 연산이 주어진 비트 수로 표현할 수 있는 범위를 벗어나는 숫자 값을 생성하려고 할 때 발생한다. 이는 최댓값보다 크거나 최솟값보다 작은 값을 나타내려고 할 때 발생한다.

 

방지하려면 safeMath 라이브러리를 사용하면 된다.!

그리고 솔리디티 버전 0.8.0 이상부터는 자체적으로 overflow / underflow처리를 해준다. 

https://docs.soliditylang.org/en/v0.8.9/080-breaking-changes.html

 

 

[ 참고 및 추가자료 ]

1. https://swcregistry.io/docs/SWC-101/

2. https://github.com/ConsenSysMesh/openzeppelin-solidity/blob/master/contracts/math/SafeMath.sol

3. https://medium.com/hexlant/safemath%EB%8A%94-%EC%99%9C-%EC%8D%A8%EC%95%BC%ED%95%98%EB%8A%94%EA%B0%80-e176ef39d59d

4. https://docs.soliditylang.org/en/v0.8.9/080-breaking-changes.html