From 63531aa375c9ec83692f919e5e2c07eeb85e6d9b Mon Sep 17 00:00:00 2001 From: arkpar Date: Mon, 10 Aug 2015 20:31:47 +0200 Subject: [PATCH] static asserts for byte inference functions (SEC-014) --- alethzero/Debugger.cpp | 4 ++-- libdevcore/CommonData.h | 4 ++++ mix/ContractCallDataEncoder.cpp | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/alethzero/Debugger.cpp b/alethzero/Debugger.cpp index fafa25b2e..a65bfc11c 100644 --- a/alethzero/Debugger.cpp +++ b/alethzero/Debugger.cpp @@ -345,7 +345,7 @@ void Debugger::on_dump_clicked() ofstream f(fn.toStdString()); if (f.is_open()) for (WorldState const& ws: m_session.history) - f << ws.cur << " " << hex << toHex(dev::toCompactBigEndian(ws.curPC, 1)) << " " << hex << toHex(dev::toCompactBigEndian((int)(byte)ws.inst, 1)) << " " << hex << toHex(dev::toCompactBigEndian((uint64_t)ws.gas, 1)) << endl; + f << ws.cur << " " << hex << toHex(dev::toCompactBigEndian(ws.curPC, 1)) << " " << hex << toHex(dev::toCompactBigEndian((unsigned)(byte)ws.inst, 1)) << " " << hex << toHex(dev::toCompactBigEndian((uint64_t)ws.gas, 1)) << endl; } void Debugger::on_dumpPretty_clicked() @@ -376,6 +376,6 @@ void Debugger::on_dumpStorage_clicked() if (ws.inst == Instruction::STOP || ws.inst == Instruction::RETURN || ws.inst == Instruction::SUICIDE) for (auto i: ws.storage) f << toHex(dev::toCompactBigEndian(i.first, 1)) << " " << toHex(dev::toCompactBigEndian(i.second, 1)) << endl; - f << ws.cur << " " << hex << toHex(dev::toCompactBigEndian(ws.curPC, 1)) << " " << hex << toHex(dev::toCompactBigEndian((int)(byte)ws.inst, 1)) << " " << hex << toHex(dev::toCompactBigEndian((uint64_t)ws.gas, 1)) << endl; + f << ws.cur << " " << hex << toHex(dev::toCompactBigEndian(ws.curPC, 1)) << " " << hex << toHex(dev::toCompactBigEndian((unsigned)(byte)ws.inst, 1)) << " " << hex << toHex(dev::toCompactBigEndian((uint64_t)ws.gas, 1)) << endl; } } diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index 5f4b0a39e..016faff6f 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -104,6 +104,7 @@ bytes asNibbles(bytesConstRef const& _s); template inline void toBigEndian(T _val, Out& o_out) { + static_assert(std::is_same::value || !std::numeric_limits::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift for (auto i = o_out.size(); i != 0; _val >>= 8, i--) { T v = _val & (T)0xff; @@ -134,6 +135,7 @@ inline bytes toBigEndian(u160 _val) { bytes ret(20); toBigEndian(_val, ret); ret template inline bytes toCompactBigEndian(T _val, unsigned _min = 0) { + static_assert(std::is_same::value || !std::numeric_limits::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift int i = 0; for (T v = _val; v; ++i, v >>= 8) {} bytes ret(std::max(_min, i), 0); @@ -150,6 +152,7 @@ inline bytes toCompactBigEndian(byte _val, unsigned _min = 0) template inline std::string toCompactBigEndianString(T _val, unsigned _min = 0) { + static_assert(std::is_same::value || !std::numeric_limits::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift int i = 0; for (T v = _val; v; ++i, v >>= 8) {} std::string ret(std::max(_min, i), '\0'); @@ -199,6 +202,7 @@ std::string randomWord(); template inline unsigned bytesRequired(T _i) { + static_assert(std::is_same::value || !std::numeric_limits::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift unsigned i = 0; for (; _i != 0; ++i, _i >>= 8) {} return i; diff --git a/mix/ContractCallDataEncoder.cpp b/mix/ContractCallDataEncoder.cpp index ed215d342..29bcbba8e 100644 --- a/mix/ContractCallDataEncoder.cpp +++ b/mix/ContractCallDataEncoder.cpp @@ -104,7 +104,7 @@ void ContractCallDataEncoder::encode(QVariant const& _data, SolidityType const& m_dynamicData += empty; //reserve space for count encodeSingleItem(_data.toString(), _type, m_dynamicData); vector_ref sizeRef(m_dynamicData.data() + sizePos, 32); - toBigEndian(_data.toString().size(), sizeRef); + toBigEndian(static_cast(_data.toString().size()), sizeRef); m_staticOffsetMap.push_back(std::make_pair(m_encodedData.size(), sizePos)); m_encodedData += empty; //reserve space for offset }