Description
Page
https://docs.soliditylang.org/en/latest/abi-spec.html#examples
Abstract
Encoding process of bytes and string is confusing(wrong). In the first example function bar(bytes3[2] memory)
, two arguments are passed ["abc", "def"]
which are getting encoded to their ASCII equivalent and get right padded. This suggests, function signature should look like bar(string[2] memory)
as mentioned in Formal Specification of the Encoding for string except for the length part.
string:
enc(X) = enc(enc_utf8(X)), i.e. X is UTF-8 encoded and this value is interpreted as of bytes type and encoded further. Note that the length used in this subsequent encoding is the number of bytes of the UTF-8 encoded string, not its number of characters.
Otherwise, if the function signature is bar(bytes3[2] memory)
then directly bytes should be passed into functions which would be 616263
and 646566
. After encoding these would become 0x6162630000000000000000000000000000000000000000000000000000000000
and 0x6465660000000000000000000000000000000000000000000000000000000000
Activity
Amit0617 commentedon Jul 26, 2024
Similar issue with third example too for
dave
.github-actions commentedon Oct 28, 2024
This issue has been marked as stale due to inactivity for the last 90 days.
It will be automatically closed in 7 days.
[-]Unclear documentation for bytes encoding[/-][+]Inaccurate documentation for bytes encoding[/+]Amit0617 commentedon Oct 29, 2024
It is simply wrong, string values are passed as bytes in those examples. I had raised this issue on foundry too, if they are lacking functionality and probably lacking specifications compliance. But this is just problematic technically, mixing string and bytes will create inconsistent encoding values. For example, if a function receives
abc
as input, it can be already hex encoded bytes value, and it can be a valid string also. This creates confusion whether it is already encoded value and needs to be padded only or encoding still needs to be done on the given value.