Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit 0 encoding failure #42

Open
innim98 opened this issue Dec 20, 2023 · 0 comments
Open

Unit 0 encoding failure #42

innim98 opened this issue Dec 20, 2023 · 0 comments

Comments

@innim98
Copy link

innim98 commented Dec 20, 2023

"_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 pushed a commit to innim98/solidity-protobuf that referenced this issue Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant