ApeWorX / ape

The smart contract development tool for Pythonistas, Data Scientists, and Security Professionals
https://apeworx.io
Apache License 2.0
867 stars 133 forks source link

Issue with string concat and decoding the value #738

Closed antazoey closed 2 years ago

antazoey commented 2 years ago

Environment information

What went wrong?

OK, so this is a bit of weird edge case that I found. I have a solidity method like this:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract ShouldFail {
    string public concatres = "";

    function methodB1(bytes32 lolol) public {
        string memory converted_lols = string(abi.encodePacked(lolol));
        string memory result = string.concat(converted_lols, " Captain Janeway");
        concatres = result;
    }
}

It takes a bytes32, converts it to a string, and then concats another string (" Captain Janeway") to the end of it. Then, it stores it in a public string getter.

I go to check the result in my Pythons and this is what happens:

contract_b.methodB1("aabb", sender=owner)  # Succeeds
contract_b.concatres()  # Fails

Failure message:

DecodingError: ('utf-8', b'\xaa\xbb\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 Captain Janeway', 0, 1, 'The returned type for this function is string which is expected to be a UTF8 encoded string of text. The returned value could not be decoded as valid UTF8. This is indicative of a broken application which is using incorrect return types for binary data.')

Solidity seems to pad my arguments before doing the concat.

How can it be fixed

I am not sure

antazoey commented 2 years ago

Further experimentation has shown that Solidity code does not even compile in Remix:

Invalid type for argument in the bytes.concat function call

and thus this may be a problem with solc

antazoey commented 2 years ago

I will close this as soon as I figure out where to open the issue in Solc

antazoey commented 2 years ago

I added a comment here: https://github.com/ethereum/solidity/issues/12087 for now