diff --git a/test/Metadata.cpp b/test/Metadata.cpp index c130d3462..41e98b486 100644 --- a/test/Metadata.cpp +++ b/test/Metadata.cpp @@ -22,6 +22,7 @@ #include #include #include +#include using namespace std; @@ -30,21 +31,24 @@ namespace dev namespace test { +bytes bytecodeSansMetadata(bytes const& _bytecode) +{ + unsigned size = _bytecode.size(); + if (size < 5) + return bytes{}; + size_t metadataSize = (_bytecode[size - 2] << 8) + _bytecode[size - 1]; + if (size < (metadataSize + 2)) + return bytes{}; + // Sanity check: assume the first byte is a fixed-size CBOR array with either 1 or 2 entries + unsigned char firstByte = _bytecode[size - metadataSize - 2]; + if (firstByte != 0xa1 && firstByte != 0xa2) + return bytes{}; + return bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2); +} + string bytecodeSansMetadata(string const& _bytecode) { - /// The metadata hash takes up 43 bytes (or 86 characters in hex) - /// /a165627a7a72305820([0-9a-f]{64})0029$/ - - if (_bytecode.size() < 88) - return _bytecode; - - if (_bytecode.substr(_bytecode.size() - 4, 4) != "0029") - return _bytecode; - - if (_bytecode.substr(_bytecode.size() - 86, 18) != "a165627a7a72305820") - return _bytecode; - - return _bytecode.substr(0, _bytecode.size() - 86); + return toHex(bytecodeSansMetadata(fromHex(_bytecode, WhenError::Throw))); } bool isValidMetadata(string const& _metadata) diff --git a/test/Metadata.h b/test/Metadata.h index cd92ecd80..113a54bee 100644 --- a/test/Metadata.h +++ b/test/Metadata.h @@ -19,6 +19,7 @@ * Metadata processing helpers. */ +#include #include namespace dev @@ -27,6 +28,10 @@ namespace test { /// Returns the bytecode with the metadata hash stripped out. +bytes bytecodeSansMetadata(bytes const& _bytecode); + +/// Returns the bytecode with the metadata hash stripped out. +/// Throws exception on invalid hex string. std::string bytecodeSansMetadata(std::string const& _bytecode); /// Expects a serialised metadata JSON and returns true if the diff --git a/test/libsolidity/LibSolc.cpp b/test/libsolidity/LibSolc.cpp index 89a502100..b94486ac8 100644 --- a/test/libsolidity/LibSolc.cpp +++ b/test/libsolidity/LibSolc.cpp @@ -25,9 +25,6 @@ #include #include -#include -#include - using namespace std; namespace dev diff --git a/test/libsolidity/SolidityOptimizer.cpp b/test/libsolidity/SolidityOptimizer.cpp index 9c6f8f65e..22ca9182f 100644 --- a/test/libsolidity/SolidityOptimizer.cpp +++ b/test/libsolidity/SolidityOptimizer.cpp @@ -20,6 +20,7 @@ * Tests for the Solidity optimizer. */ +#include #include #include @@ -105,11 +106,8 @@ public: /// into account. size_t numInstructions(bytes const& _bytecode, boost::optional _which = boost::optional{}) { - BOOST_REQUIRE(_bytecode.size() > 5); - size_t metadataSize = (_bytecode[_bytecode.size() - 2] << 8) + _bytecode[_bytecode.size() - 1]; - BOOST_REQUIRE_MESSAGE(metadataSize == 0x29, "Invalid metadata size"); - BOOST_REQUIRE(_bytecode.size() >= metadataSize + 2); - bytes realCode = bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2); + bytes realCode = bytecodeSansMetadata(_bytecode); + BOOST_REQUIRE_MESSAGE(!realCode.empty(), "Invalid or missing metadata in bytecode."); size_t instructions = 0; solidity::eachInstruction(realCode, [&](Instruction _instr, u256 const&) { if (!_which || *_which == _instr) diff --git a/test/libsolidity/StandardCompiler.cpp b/test/libsolidity/StandardCompiler.cpp index 118d8210c..0cd395860 100644 --- a/test/libsolidity/StandardCompiler.cpp +++ b/test/libsolidity/StandardCompiler.cpp @@ -23,8 +23,7 @@ #include #include #include - -#include "../Metadata.h" +#include using namespace std; using namespace dev::eth;