Skip to content

Commit

Permalink
Merge pull request #3624 from ethereum/jsonio-libraries-hex
Browse files Browse the repository at this point in the history
 Ensure that library addresses supplied are of correct length and hex prefixed
  • Loading branch information
axic authored Mar 1, 2018
2 parents c123fa2 + b6dfd9e commit 2c82f74
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Bugfixes:
* JSON-AST: Add "documentation" property to function, event and modifier definition.
* Resolver: Properly determine shadowing for imports with aliases.
* Standalone Assembly: Do not ignore input after closing brace of top level block.
* Standard JSON: catch errors properly when invalid "sources" are passed
* Standard JSON: Catch errors properly when invalid "sources" are passed.
* Standard JSON: Ensure that library addresses supplied are of correct length and hex prefixed.
* Type Checker: Properly warn when using ``_offset`` and ``_slot`` for constants in inline assembly.
* Commandline interface: throw error if option is unknown

Expand Down
20 changes: 18 additions & 2 deletions libsolidity/interface/StandardCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <libdevcore/JSON.h>
#include <libdevcore/SHA3.h>

#include <boost/algorithm/string.hpp>

using namespace std;
using namespace dev;
using namespace dev::solidity;
Expand Down Expand Up @@ -337,16 +339,30 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
return formatFatalError("JSONError", "library entry is not a JSON object.");
for (auto const& library: jsonSourceName.getMemberNames())
{
string address = jsonSourceName[library].asString();

if (!boost::starts_with(address, "0x"))
return formatFatalError(
"JSONError",
"Library address is not prefixed with \"0x\"."
);

if (address.length() != 42)
return formatFatalError(
"JSONError",
"Library address is of invalid length."
);

try
{
// @TODO use libraries only for the given source
libraries[library] = h160(jsonSourceName[library].asString());
libraries[library] = h160(address);
}
catch (dev::BadHexCharacter)
{
return formatFatalError(
"JSONError",
"Invalid library address (\"" + jsonSourceName[library].asString() + "\") supplied."
"Invalid library address (\"" + address + "\") supplied."
);
}
}
Expand Down
34 changes: 27 additions & 7 deletions test/libsolidity/StandardCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,19 +633,16 @@ BOOST_AUTO_TEST_CASE(libraries_invalid_hex)
BOOST_CHECK(containsError(result, "JSONError", "Invalid library address (\"0x4200000000000000000000000000000000000xx1\") supplied."));
}

BOOST_AUTO_TEST_CASE(libraries_various_addresses)
BOOST_AUTO_TEST_CASE(libraries_invalid_length)
{
char const* input = R"(
{
"language": "Solidity",
"settings": {
"libraries": {
"library.sol": {
"L": 42,
"L3": "42",
"L4": "0x42",
"L5": "0x4200000000000000000000000000000000000001",
"L6": "4200000000000000000000000000000000000001"
"L1": "0x42",
"L2": "0x4200000000000000000000000000000000000001ff"
}
}
},
Expand All @@ -657,7 +654,30 @@ BOOST_AUTO_TEST_CASE(libraries_various_addresses)
}
)";
Json::Value result = compile(input);
BOOST_CHECK(containsAtMostWarnings(result));
BOOST_CHECK(containsError(result, "JSONError", "Library address is of invalid length."));
}

BOOST_AUTO_TEST_CASE(libraries_missing_hex_prefix)
{
char const* input = R"(
{
"language": "Solidity",
"settings": {
"libraries": {
"library.sol": {
"L": "4200000000000000000000000000000000000001"
}
}
},
"sources": {
"empty": {
"content": ""
}
}
}
)";
Json::Value result = compile(input);
BOOST_CHECK(containsError(result, "JSONError", "Library address is not prefixed with \"0x\"."));
}

BOOST_AUTO_TEST_CASE(library_linking)
Expand Down

0 comments on commit 2c82f74

Please sign in to comment.