Skip to content

Instantly share code, notes, and snippets.

@andrewgordstewart
Created September 10, 2019 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewgordstewart/6fa284c4ef02e4b7063fbbba7a5f8594 to your computer and use it in GitHub Desktop.
Save andrewgordstewart/6fa284c4ef02e4b7063fbbba7a5f8594 to your computer and use it in GitHub Desktop.
pragma solidity ^0.5.11;
pragma experimental ABIEncoderV2;
contract ItemDefiner {
struct Item {
bool isGood;
}
}
contract ValidItemChecker {
function checkIfIsGood(bytes memory itemBytes) public pure returns (bool) {
// This function shows valid solidity (which compiles) that is also successfully
// parsed.
ItemDefiner.Item memory item = abi.decode(itemBytes, (ItemDefiner.Item));
return item.isGood;
}
function checkIfArePlentiful(bytes memory itemsBytes) public pure returns (bool) {
// The following line is correct code.
// This contract will compile, but the parser fails to parse it.
ItemDefiner.Item[] memory items = abi.decode(itemsBytes, (ItemDefiner.Item[]));
return items.length > 5;
}
}
contract InvalidItemChecker {
function checkIfIsGood(bytes memory itemBytes) public pure returns (bool) {
ItemDefiner.Item memory item = abi.decode(itemBytes, (ItemDefiner.Item));
return item.isGood;
}
function checkIfArePlentiful(bytes memory itemsBytes) public pure returns (bool) {
// The following line is incorrect code: abi.decode will try to assign
// a value of type ItemDefiner.Item to a variable of type ItemDefiner.Item[]
// This contract will not compile, but the parser parses it as valid code.
ItemDefiner.Item[] memory items = abi.decode(itemsBytes, (ItemDefiner.Item));
return items.length > 5;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment