Skip to content

Unit 0 encoding failure #42

@innim98

Description

@innim98

"_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;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions