Browse Source

static asserts for byte inference functions (SEC-014)

cl-refactor
arkpar 10 years ago
parent
commit
63531aa375
  1. 4
      alethzero/Debugger.cpp
  2. 4
      libdevcore/CommonData.h
  3. 2
      mix/ContractCallDataEncoder.cpp

4
alethzero/Debugger.cpp

@ -345,7 +345,7 @@ void Debugger::on_dump_clicked()
ofstream f(fn.toStdString()); ofstream f(fn.toStdString());
if (f.is_open()) if (f.is_open())
for (WorldState const& ws: m_session.history) 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() 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) if (ws.inst == Instruction::STOP || ws.inst == Instruction::RETURN || ws.inst == Instruction::SUICIDE)
for (auto i: ws.storage) for (auto i: ws.storage)
f << toHex(dev::toCompactBigEndian(i.first, 1)) << " " << toHex(dev::toCompactBigEndian(i.second, 1)) << endl; 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;
} }
} }

4
libdevcore/CommonData.h

@ -104,6 +104,7 @@ bytes asNibbles(bytesConstRef const& _s);
template <class T, class Out> template <class T, class Out>
inline void toBigEndian(T _val, Out& o_out) inline void toBigEndian(T _val, Out& o_out)
{ {
static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::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--) for (auto i = o_out.size(); i != 0; _val >>= 8, i--)
{ {
T v = _val & (T)0xff; T v = _val & (T)0xff;
@ -134,6 +135,7 @@ inline bytes toBigEndian(u160 _val) { bytes ret(20); toBigEndian(_val, ret); ret
template <class T> template <class T>
inline bytes toCompactBigEndian(T _val, unsigned _min = 0) inline bytes toCompactBigEndian(T _val, unsigned _min = 0)
{ {
static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
int i = 0; int i = 0;
for (T v = _val; v; ++i, v >>= 8) {} for (T v = _val; v; ++i, v >>= 8) {}
bytes ret(std::max<unsigned>(_min, i), 0); bytes ret(std::max<unsigned>(_min, i), 0);
@ -150,6 +152,7 @@ inline bytes toCompactBigEndian(byte _val, unsigned _min = 0)
template <class T> template <class T>
inline std::string toCompactBigEndianString(T _val, unsigned _min = 0) inline std::string toCompactBigEndianString(T _val, unsigned _min = 0)
{ {
static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
int i = 0; int i = 0;
for (T v = _val; v; ++i, v >>= 8) {} for (T v = _val; v; ++i, v >>= 8) {}
std::string ret(std::max<unsigned>(_min, i), '\0'); std::string ret(std::max<unsigned>(_min, i), '\0');
@ -199,6 +202,7 @@ std::string randomWord();
template <class T> template <class T>
inline unsigned bytesRequired(T _i) inline unsigned bytesRequired(T _i)
{ {
static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
unsigned i = 0; unsigned i = 0;
for (; _i != 0; ++i, _i >>= 8) {} for (; _i != 0; ++i, _i >>= 8) {}
return i; return i;

2
mix/ContractCallDataEncoder.cpp

@ -104,7 +104,7 @@ void ContractCallDataEncoder::encode(QVariant const& _data, SolidityType const&
m_dynamicData += empty; //reserve space for count m_dynamicData += empty; //reserve space for count
encodeSingleItem(_data.toString(), _type, m_dynamicData); encodeSingleItem(_data.toString(), _type, m_dynamicData);
vector_ref<byte> sizeRef(m_dynamicData.data() + sizePos, 32); vector_ref<byte> sizeRef(m_dynamicData.data() + sizePos, 32);
toBigEndian(_data.toString().size(), sizeRef); toBigEndian(static_cast<unsigned>(_data.toString().size()), sizeRef);
m_staticOffsetMap.push_back(std::make_pair(m_encodedData.size(), sizePos)); m_staticOffsetMap.push_back(std::make_pair(m_encodedData.size(), sizePos));
m_encodedData += empty; //reserve space for offset m_encodedData += empty; //reserve space for offset
} }

Loading…
Cancel
Save