Browse Source

Merge pull request #1016 from CJentzsch/ConversionSafety

Conversion safety
cl-refactor
Gav Wood 10 years ago
parent
commit
9a351e849c
  1. 34
      alethzero/MainWin.cpp
  2. 90
      eth/main.cpp
  3. 6
      libdevcore/CommonData.cpp
  4. 9
      libdevcore/CommonData.h
  5. 73
      neth/main.cpp

34
alethzero/MainWin.cpp

@ -569,7 +569,23 @@ Address Main::fromString(QString const& _n) const
}*/ }*/
if (_n.size() == 40) if (_n.size() == 40)
return Address(fromHex(_n.toStdString())); {
try
{
return Address(fromHex(_n.toStdString(), ThrowType::Throw));
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, address rejected";
cwarn << boost::diagnostic_information(_e);
return Address();
}
catch (...)
{
cwarn << "address rejected";
return Address();
}
}
else else
return Address(); return Address();
} }
@ -1327,8 +1343,20 @@ void Main::ourAccountsRowsMoved()
void Main::on_inject_triggered() void Main::on_inject_triggered()
{ {
QString s = QInputDialog::getText(this, "Inject Transaction", "Enter transaction dump in hex"); QString s = QInputDialog::getText(this, "Inject Transaction", "Enter transaction dump in hex");
bytes b = fromHex(s.toStdString()); try
ethereum()->inject(&b); {
bytes b = fromHex(s.toStdString(), ThrowType::Throw);
ethereum()->inject(&b);
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, transaction rejected";
cwarn << boost::diagnostic_information(_e);
}
catch (...)
{
cwarn << "transaction rejected";
}
} }
void Main::on_blocks_currentItemChanged() void Main::on_blocks_currentItemChanged()

90
eth/main.cpp

@ -258,7 +258,23 @@ int main(int argc, char** argv)
else if ((arg == "-c" || arg == "--client-name") && i + 1 < argc) else if ((arg == "-c" || arg == "--client-name") && i + 1 < argc)
clientName = argv[++i]; clientName = argv[++i];
else if ((arg == "-a" || arg == "--address" || arg == "--coinbase-address") && i + 1 < argc) else if ((arg == "-a" || arg == "--address" || arg == "--coinbase-address") && i + 1 < argc)
coinbase = h160(fromHex(argv[++i])); {
try
{
coinbase = h160(fromHex(argv[++i], ThrowType::Throw));
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, coinbase rejected";
cwarn << boost::diagnostic_information(_e);
break;
}
catch (...)
{
cwarn << "coinbase rejected";
break;
}
}
else if ((arg == "-s" || arg == "--secret") && i + 1 < argc) else if ((arg == "-s" || arg == "--secret") && i + 1 < argc)
us = KeyPair(h256(fromHex(argv[++i]))); us = KeyPair(h256(fromHex(argv[++i])));
else if ((arg == "-d" || arg == "--path" || arg == "--db-path") && i + 1 < argc) else if ((arg == "-d" || arg == "--path" || arg == "--db-path") && i + 1 < argc)
@ -532,9 +548,21 @@ int main(int argc, char** argv)
} }
else else
{ {
Secret secret = h256(fromHex(sechex)); try
Address dest = h160(fromHex(hexAddr)); {
c->transact(secret, amount, dest, data, gas, gasPrice); Secret secret = h256(fromHex(sechex));
Address dest = h160(fromHex(hexAddr));
c->transact(secret, amount, dest, data, gas, gasPrice);
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, transaction rejected";
cwarn << boost::diagnostic_information(_e);
}
catch (...)
{
cwarn << "transaction rejected";
}
} }
} }
else else
@ -583,8 +611,20 @@ int main(int argc, char** argv)
auto blockData = bc.block(h); auto blockData = bc.block(h);
BlockInfo info(blockData); BlockInfo info(blockData);
u256 minGas = (u256)Client::txGas(bytes(), 0); u256 minGas = (u256)Client::txGas(bytes(), 0);
Address dest = h160(fromHex(hexAddr)); try
c->transact(us.secret(), amount, dest, bytes(), minGas); {
Address dest = h160(fromHex(hexAddr, ThrowType::Throw));
c->transact(us.secret(), amount, dest, bytes(), minGas);
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, transaction rejected";
cwarn << boost::diagnostic_information(_e);
}
catch (...)
{
cwarn << "transaction rejected";
}
} }
} }
else else
@ -615,14 +655,30 @@ int main(int argc, char** argv)
{ {
cnote << "Assembled:"; cnote << "Assembled:";
stringstream ssc; stringstream ssc;
init = fromHex(sinit); try
{
init = fromHex(sinit, ThrowType::Throw);
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, code rejected";
cwarn << boost::diagnostic_information(_e);
init = bytes();
}
catch (...)
{
cwarn << "code rejected";
init = bytes();
}
ssc.str(string()); ssc.str(string());
ssc << disassemble(init); ssc << disassemble(init);
cnote << "Init:"; cnote << "Init:";
cnote << ssc.str(); cnote << ssc.str();
} }
u256 minGas = (u256)Client::txGas(init, 0); u256 minGas = (u256)Client::txGas(init, 0);
if (endowment < 0) if (!init.size())
cwarn << "Contract creation aborted, no init code.";
else if (endowment < 0)
cwarn << "Invalid endowment"; cwarn << "Invalid endowment";
else if (gas < minGas) else if (gas < minGas)
cwarn << "Minimum gas amount is" << minGas; cwarn << "Minimum gas amount is" << minGas;
@ -759,8 +815,22 @@ int main(int argc, char** argv)
if (hexAddr.length() != 40) if (hexAddr.length() != 40)
cwarn << "Invalid address length: " << hexAddr.length(); cwarn << "Invalid address length: " << hexAddr.length();
else else
coinbase = h160(fromHex(hexAddr)); {
} try
{
coinbase = h160(fromHex(hexAddr, ThrowType::Throw));
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, coinbase rejected";
cwarn << boost::diagnostic_information(_e);
}
catch (...)
{
cwarn << "coinbase rejected";
}
}
}
else else
cwarn << "Require parameter: setAddress HEXADDRESS"; cwarn << "Require parameter: setAddress HEXADDRESS";
} }

6
libdevcore/CommonData.cpp

@ -78,7 +78,7 @@ int dev::fromHex(char _i)
BOOST_THROW_EXCEPTION(BadHexCharacter() << errinfo_invalidSymbol(_i)); BOOST_THROW_EXCEPTION(BadHexCharacter() << errinfo_invalidSymbol(_i));
} }
bytes dev::fromHex(std::string const& _s) bytes dev::fromHex(std::string const& _s, ThrowType _throw)
{ {
unsigned s = (_s[0] == '0' && _s[1] == 'x') ? 2 : 0; unsigned s = (_s[0] == '0' && _s[1] == 'x') ? 2 : 0;
std::vector<uint8_t> ret; std::vector<uint8_t> ret;
@ -96,6 +96,8 @@ bytes dev::fromHex(std::string const& _s)
#ifndef BOOST_NO_EXCEPTIONS #ifndef BOOST_NO_EXCEPTIONS
cwarn << boost::current_exception_diagnostic_information(); cwarn << boost::current_exception_diagnostic_information();
#endif #endif
if (_throw == ThrowType::Throw)
throw;
} }
for (unsigned i = s; i < _s.size(); i += 2) for (unsigned i = s; i < _s.size(); i += 2)
try try
@ -107,6 +109,8 @@ bytes dev::fromHex(std::string const& _s)
#ifndef BOOST_NO_EXCEPTIONS #ifndef BOOST_NO_EXCEPTIONS
cwarn << boost::current_exception_diagnostic_information(); cwarn << boost::current_exception_diagnostic_information();
#endif #endif
if (_throw == ThrowType::Throw)
throw;
} }
return ret; return ret;
} }

9
libdevcore/CommonData.h

@ -35,6 +35,12 @@ namespace dev
// String conversion functions, mainly to/from hex/nibble/byte representations. // String conversion functions, mainly to/from hex/nibble/byte representations.
enum class ThrowType
{
NoThrow = 0,
Throw = 1,
};
/// Convert a series of bytes to the corresponding string of hex duplets. /// Convert a series of bytes to the corresponding string of hex duplets.
/// @param _w specifies the width of each of the elements. Defaults to two - enough to represent a byte. /// @param _w specifies the width of each of the elements. Defaults to two - enough to represent a byte.
/// @example toHex("A\x69") == "4169" /// @example toHex("A\x69") == "4169"
@ -53,7 +59,8 @@ int fromHex(char _i);
/// Converts a (printable) ASCII hex string into the corresponding byte stream. /// Converts a (printable) ASCII hex string into the corresponding byte stream.
/// @example fromHex("41626261") == asBytes("Abba") /// @example fromHex("41626261") == asBytes("Abba")
bytes fromHex(std::string const& _s); /// If _throw = ThrowType::NoThrow, it replaces bad hex characters with 0's, otherwise it will throw an exception.
bytes fromHex(std::string const& _s, ThrowType _throw = ThrowType::NoThrow);
#if 0 #if 0
std::string toBase58(bytesConstRef _data); std::string toBase58(bytesConstRef _data);

73
neth/main.cpp

@ -366,7 +366,23 @@ int main(int argc, char** argv)
else if ((arg == "-c" || arg == "--client-name") && i + 1 < argc) else if ((arg == "-c" || arg == "--client-name") && i + 1 < argc)
clientName = argv[++i]; clientName = argv[++i];
else if ((arg == "-a" || arg == "--address" || arg == "--coinbase-address") && i + 1 < argc) else if ((arg == "-a" || arg == "--address" || arg == "--coinbase-address") && i + 1 < argc)
coinbase = h160(fromHex(argv[++i])); {
try
{
coinbase = h160(fromHex(argv[++i], ThrowType::Throw));
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, coinbase rejected";
cwarn << boost::diagnostic_information(_e);
break;
}
catch (...)
{
cwarn << "coinbase rejected";
break;
}
}
else if ((arg == "-s" || arg == "--secret") && i + 1 < argc) else if ((arg == "-s" || arg == "--secret") && i + 1 < argc)
us = KeyPair(h256(fromHex(argv[++i]))); us = KeyPair(h256(fromHex(argv[++i])));
else if ((arg == "-d" || arg == "--path" || arg == "--db-path") && i + 1 < argc) else if ((arg == "-d" || arg == "--path" || arg == "--db-path") && i + 1 < argc)
@ -709,9 +725,21 @@ int main(int argc, char** argv)
} }
else else
{ {
Secret secret = h256(fromHex(sechex)); try
Address dest = h160(fromHex(fields[0])); {
c->transact(secret, amount, dest, data, gas, gasPrice); Secret secret = h256(fromHex(sechex, ThrowType::Throw));
Address dest = h160(fromHex(fields[0], ThrowType::Throw));
c->transact(secret, amount, dest, data, gas, gasPrice);
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, transaction rejected";
cwarn << boost::diagnostic_information(_e);
}
catch (...)
{
cwarn << "transaction rejected";
}
} }
} }
} }
@ -749,8 +777,20 @@ int main(int argc, char** argv)
auto blockData = bc.block(h); auto blockData = bc.block(h);
BlockInfo info(blockData); BlockInfo info(blockData);
u256 minGas = (u256)Client::txGas(bytes(), 0); u256 minGas = (u256)Client::txGas(bytes(), 0);
Address dest = h160(fromHex(fields[0])); try
c->transact(us.secret(), amount, dest, bytes(), minGas); {
Address dest = h160(fromHex(fields[0], ThrowType::Throw));
c->transact(us.secret(), amount, dest, bytes(), minGas);
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, transaction rejected";
cwarn << boost::diagnostic_information(_e);
}
catch (...)
{
cwarn << "transaction rejected";
}
} }
} }
} }
@ -803,14 +843,31 @@ int main(int argc, char** argv)
{ {
cnote << "Assembled:"; cnote << "Assembled:";
stringstream ssc; stringstream ssc;
init = fromHex(sinit); try
{
init = fromHex(sinit, ThrowType::Throw);
}
catch (BadHexCharacter& _e)
{
cwarn << "invalid hex character, code rejected";
cwarn << boost::diagnostic_information(_e);
init = bytes();
}
catch (...)
{
cwarn << "code rejected";
init = bytes();
}
ssc.str(string()); ssc.str(string());
ssc << disassemble(init); ssc << disassemble(init);
cnote << "Init:"; cnote << "Init:";
cnote << ssc.str(); cnote << ssc.str();
} }
u256 minGas = (u256)Client::txGas(init, 0); u256 minGas = (u256)Client::txGas(init, 0);
if (endowment < 0) if (!init.size())
cwarn << "Contract creation aborted, no init code.";
else if (endowment < 0)
cwarn << "Invalid endowment"; cwarn << "Invalid endowment";
else if (gas < minGas) else if (gas < minGas)
cwarn << "Minimum gas amount is" << minGas; cwarn << "Minimum gas amount is" << minGas;

Loading…
Cancel
Save