1

My string to bytes32, bytes32 to string code is below.

  function _stringToBytes32(string _string)
    internal
    pure
    returns (bytes32 _bytes32)
  {
    require(bytes(_string).length < 33);
    assembly {
      _bytes32 := mload(add(_string, 0x20))
    }
  }

  function _bytes32ToString(bytes32 _bytes32)
    internal
    pure
    returns (string _string)
  {
    assembly {
      let m := mload(0x40)
      mstore(m, 0x20)
      mstore(add(m, 0x20), _bytes32)
      mstore(0x40, add(m, 0x40))
      _string := m
    }
  }

I find the warning from remix,

static analysis modules do not parse inline Assembly, this can lead to wrong analysis results.

And on solidity document,

Statically-sized memory arrays do not have a length field, but it will be added soon to allow better convertibility between statically- and dynamically-sized arrays, so please do not rely on that.

It seems that this is not the case of my code. However, I want to know how much risk is relying on this assembly code.

My functions are rely on two facts,

  1. Under 33 length alphanumeric string has 32bytes length field + 32bytes body field.
  2. Bytes32 has 32bytes body field only.
  • I believe that the static analysis warning is just saying that it can't tell if there's anything wrong in that function because it doesn't understand assembly. "Sorry, you're on your own"-style. – norganna Mar 20 '18 at 13:25

Your Answer

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

Browse other questions tagged or ask your own question.