provable-things / ethereum-api

Provable API for Ethereum smart contracts
https://docs.provable.xyz/#ethereum
MIT License
800 stars 428 forks source link

uint2str underflows when 1 digit #105

Open aelmanaa opened 3 years ago

aelmanaa commented 3 years ago

https://github.com/provable-things/ethereum-api/blob/9f34daaa550202c44f48cdee7754245074bde65d/provableAPI_0.6.sol#L1041

Hi

I have few revert issues when I tried to format 1 digit numbers (1 ,2 ,3..Etc) .

uint k = len - 1; --> length is 1 and k is 0

bstr[k--] = byte(uint8(48 + _i % 10)); underflows as k is unsigned so 0-1 = -1

suggestion to have something like this. "k" starts with 1 and is decremented before operation

        uint k = len ;
        while (_i != 0) {
            bstr[--k] = bytes1(uint8(48 + _i % 10));
            _i /= 10;
        }
        return string(bstr);