diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index e5dae5644..907eda568 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -569,7 +569,23 @@ Address Main::fromString(QString const& _n) const }*/ 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 return Address(); } @@ -1327,24 +1343,20 @@ void Main::ourAccountsRowsMoved() void Main::on_inject_triggered() { QString s = QInputDialog::getText(this, "Inject Transaction", "Enter transaction dump in hex"); - bytes b; try { - b = fromHex(s.toStdString(), true); + bytes b = fromHex(s.toStdString(), ThrowType::Throw); + ethereum()->inject(&b); } catch (BadHexCharacter& _e) { - cnote << "invalid hex character, transaction rejected"; - cnote << boost::diagnostic_information(_e); - return; + cwarn << "invalid hex character, transaction rejected"; + cwarn << boost::diagnostic_information(_e); } catch (...) { - cnote << "transaction rejected"; - return; + cwarn << "transaction rejected"; } - - ethereum()->inject(&b); } void Main::on_blocks_currentItemChanged() diff --git a/eth/main.cpp b/eth/main.cpp index 7a128298a..1c72b7036 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -258,7 +258,23 @@ int main(int argc, char** argv) else if ((arg == "-c" || arg == "--client-name") && i + 1 < argc) clientName = argv[++i]; 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) us = KeyPair(h256(fromHex(argv[++i]))); else if ((arg == "-d" || arg == "--path" || arg == "--db-path") && i + 1 < argc) @@ -532,9 +548,21 @@ int main(int argc, char** argv) } else { - Secret secret = h256(fromHex(sechex)); - Address dest = h160(fromHex(hexAddr)); - c->transact(secret, amount, dest, data, gas, gasPrice); + try + { + 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 @@ -583,8 +611,20 @@ int main(int argc, char** argv) auto blockData = bc.block(h); BlockInfo info(blockData); u256 minGas = (u256)Client::txGas(bytes(), 0); - Address dest = h160(fromHex(hexAddr)); - c->transact(us.secret(), amount, dest, bytes(), minGas); + try + { + 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 @@ -615,14 +655,30 @@ int main(int argc, char** argv) { cnote << "Assembled:"; 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 << disassemble(init); cnote << "Init:"; cnote << ssc.str(); } 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"; else if (gas < minGas) cwarn << "Minimum gas amount is" << minGas; @@ -759,8 +815,22 @@ int main(int argc, char** argv) if (hexAddr.length() != 40) cwarn << "Invalid address length: " << hexAddr.length(); 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 cwarn << "Require parameter: setAddress HEXADDRESS"; } diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index c82d0b7cb..3cdf3b449 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -59,7 +59,7 @@ int fromHex(char _i); /// Converts a (printable) ASCII hex string into the corresponding byte stream. /// @example fromHex("41626261") == asBytes("Abba") -/// If _throw = false, it replaces bad hex characters with 0's, otherwise it will throw an exception. +/// 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 diff --git a/neth/main.cpp b/neth/main.cpp index 2c1a85241..efede7bb2 100644 --- a/neth/main.cpp +++ b/neth/main.cpp @@ -366,7 +366,23 @@ int main(int argc, char** argv) else if ((arg == "-c" || arg == "--client-name") && i + 1 < argc) clientName = argv[++i]; 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) us = KeyPair(h256(fromHex(argv[++i]))); else if ((arg == "-d" || arg == "--path" || arg == "--db-path") && i + 1 < argc) @@ -709,9 +725,21 @@ int main(int argc, char** argv) } else { - Secret secret = h256(fromHex(sechex)); - Address dest = h160(fromHex(fields[0])); - c->transact(secret, amount, dest, data, gas, gasPrice); + try + { + 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); BlockInfo info(blockData); u256 minGas = (u256)Client::txGas(bytes(), 0); - Address dest = h160(fromHex(fields[0])); - c->transact(us.secret(), amount, dest, bytes(), minGas); + try + { + 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:"; 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 << disassemble(init); cnote << "Init:"; cnote << ssc.str(); } 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"; else if (gas < minGas) cwarn << "Minimum gas amount is" << minGas;