From 3a378eae1a57683d0f31ab54c122b2a5c6a7c8bb Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 5 Dec 2018 20:34:27 +0100 Subject: [PATCH] Restrict toHex to `bytes`. --- libdevcore/CommonData.h | 7 +++---- libdevcore/FixedHash.h | 2 +- libsolidity/ast/ASTJsonConverter.cpp | 2 +- libsolidity/interface/CompilerStack.cpp | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index 4118907c9..0a8c37d25 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -61,12 +61,11 @@ enum class HexCase /// Convert a series of bytes to the corresponding string of hex duplets. /// @param _w specifies the width of the first of the elements. Defaults to two - enough to represent a byte. /// @example toHex("A\x69") == "4169" -template -std::string toHex(T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower) +inline std::string toHex(bytes const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::DontAdd, HexCase _case = HexCase::Lower) { std::ostringstream ret; int rix = _data.size() - 1; - for (auto datum: _data) + for (uint8_t c: _data) { // switch hex case every four hexchars auto hexcase = std::nouppercase; @@ -76,7 +75,7 @@ std::string toHex(T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::Don hexcase = (rix-- & 2) == 0 ? std::nouppercase : std::uppercase; ret << std::hex << hexcase << std::setfill('0') << std::setw(_w) - << +static_cast::type>(datum); + << size_t(c); } return (_prefix == HexPrefix::Add) ? "0x" + ret.str() : ret.str(); diff --git a/libdevcore/FixedHash.h b/libdevcore/FixedHash.h index 24b898409..9245d7269 100644 --- a/libdevcore/FixedHash.h +++ b/libdevcore/FixedHash.h @@ -95,7 +95,7 @@ public: uint8_t operator[](unsigned _i) const { return m_data[_i]; } /// @returns the hash as a user-readable hex string. - std::string hex() const { return toHex(ref()); } + std::string hex() const { return toHex(asBytes()); } /// @returns a mutable byte vector_ref to the object's data. bytesRef ref() { return bytesRef(m_data.data(), N); } diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp index cfb13271a..e92134a83 100644 --- a/libsolidity/ast/ASTJsonConverter.cpp +++ b/libsolidity/ast/ASTJsonConverter.cpp @@ -724,7 +724,7 @@ bool ASTJsonConverter::visit(Literal const& _node) std::vector> attributes = { make_pair(m_legacy ? "token" : "kind", literalTokenKind(_node.token())), make_pair("value", value), - make_pair(m_legacy ? "hexvalue" : "hexValue", toHex(_node.value())), + make_pair(m_legacy ? "hexvalue" : "hexValue", toHex(asBytes(_node.value()))), make_pair( "subdenomination", subdenomination == Token::Illegal ? diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index 610caea1f..1e6e23300 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -552,7 +552,7 @@ Json::Value CompilerStack::methodIdentifiers(string const& _contractName) const { Json::Value methodIdentifiers(Json::objectValue); for (auto const& it: contractDefinition(_contractName).interfaceFunctions()) - methodIdentifiers[it.second->externalSignature()] = toHex(it.first.ref()); + methodIdentifiers[it.second->externalSignature()] = it.first.hex(); return methodIdentifiers; }