solidity-protobuf icon indicating copy to clipboard operation
solidity-protobuf copied to clipboard

Unit 0 encoding failure

Open innim98 opened this issue 1 year ago • 0 comments

"_get_real_size" function fails when sz == 0

The problem is in ProtoBufRuntime.sol while getting size of number to be encoded in probuf. Because realSize(sz) is zero, it runs (base << (0*BYTE_SIZE - BYTE_SIZE)) which is actually (base << -BYTE_SIZE). This code cannot be run in solidity because it doesn't support minus shift.

  /**
   * @dev Get the actual size needed to encoding an unsigned integer
   * @param x The unsigned integer to be encoded
   * @param sz The maximum number of bytes used to encode Solidity types
   * @return The number of bytes needed for encoding `x`
   */
  function _get_real_size(uint256 x, uint256 sz)
    internal
    pure
    returns (uint256)
  {
    uint256 base = 0xff;
    uint256 realSize = sz;
    while (
      x & (base << (realSize * BYTE_SIZE - BYTE_SIZE)) == 0 && realSize > 0   // This line has the problem
    ) {
      realSize -= 1;
    }
    if (realSize == 0) {
      realSize = 1;
    }
    return realSize;
  }

innim98 avatar Dec 20 '23 06:12 innim98