5

Since Solidity 0.4.3, the compiler now shows warnings when a block is not balanced. For example:

Error: Inline assembly block is not balanced. It leaves 1 item(s) on the stack.

I've tried finding documentation that gives into what this entails, and why it happens, but haven't found anything useful. What happens here? How do you fix this?

This internal getCode/codeAt function gives the above error (for example).

function codeAt(address _addr) internal returns (bytes o_code) {
  assembly {
      // retrieve the size of the code, this needs assembly
      let size := extcodesize(_addr)
      // allocate output byte array - this could also be done without assembly
      // by using o_code = new bytes(size)
      o_code := mload(0x40)
      // new "memory end" including padding
      mstore(0x40, add(o_code, and(add(add(size, 0x20), 0x1f), not(0x1f))))
      // store length in memory
      mstore(o_code, size)
      // actually retrieve the code, this needs assembly
      extcodecopy(_addr, add(o_code, 0x20), 0, size)
  }
}
  • Interesting. This could be a bug. Your code (and the example from the solidity readthedocs) compiles without error on compiler version 0.4.2 and earlier (at least to 0.4.1) but the newer compilers mention the stack imbalance. – Jamie Hale Nov 11 '16 at 5:54
  • This warning was introduced in 0.4.3. I assume it's not an error per se, but more a warning that something might be wrong if it is not balanced (whatever that means). – Simon de la Rouviere Nov 14 '16 at 8:40
  • 1
    Yes, this seems like a bug, we will investigate: github.com/ethereum/solidity/issues/1368 – chriseth Nov 14 '16 at 15:00
1

For conclusion, this was a bug introduced in Solidity 0.4.3.

It was fixed in Solidity 0.4.5.

Bugfixes:

Inline assembly: calculate stack height warning correctly even when local variables are used.

Solidity 0.4.6 should be used since it has an important bug fix in the Solidity optimizer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.