Merge pull request #7785 from ethereum/exception-cleanup

A lot of tiny cleanups to exceptions
This commit is contained in:
chriseth 2019-11-26 15:55:11 +01:00 committed by GitHub
commit 6361e55e2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 26 additions and 38 deletions

View File

@ -87,7 +87,7 @@ int dev::fromHex(char _i, WhenError _throw)
if (_i >= 'A' && _i <= 'F') if (_i >= 'A' && _i <= 'F')
return _i - 'A' + 10; return _i - 'A' + 10;
if (_throw == WhenError::Throw) if (_throw == WhenError::Throw)
BOOST_THROW_EXCEPTION(BadHexCharacter() << errinfo_invalidSymbol(_i)); assertThrow(false, BadHexCharacter, to_string(_i));
else else
return -1; return -1;
} }
@ -100,22 +100,18 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw)
if (_s.size() % 2) if (_s.size() % 2)
{ {
int h = fromHex(_s[s++], WhenError::DontThrow); int h = fromHex(_s[s++], _throw);
if (h != -1) if (h != -1)
ret.push_back(h); ret.push_back(h);
else if (_throw == WhenError::Throw)
BOOST_THROW_EXCEPTION(BadHexCharacter());
else else
return bytes(); return bytes();
} }
for (unsigned i = s; i < _s.size(); i += 2) for (unsigned i = s; i < _s.size(); i += 2)
{ {
int h = fromHex(_s[i], WhenError::DontThrow); int h = fromHex(_s[i], _throw);
int l = fromHex(_s[i + 1], WhenError::DontThrow); int l = fromHex(_s[i + 1], _throw);
if (h != -1 && l != -1) if (h != -1 && l != -1)
ret.push_back((uint8_t)(h * 16 + l)); ret.push_back((uint8_t)(h * 16 + l));
else if (_throw == WhenError::Throw)
BOOST_THROW_EXCEPTION(BadHexCharacter());
else else
return bytes(); return bytes();
} }

View File

@ -51,7 +51,6 @@ DEV_SIMPLE_EXCEPTION(FileError);
DEV_SIMPLE_EXCEPTION(DataTooLong); DEV_SIMPLE_EXCEPTION(DataTooLong);
// error information to be added to exceptions // error information to be added to exceptions
using errinfo_invalidSymbol = boost::error_info<struct tag_invalidSymbol, char>;
using errinfo_comment = boost::error_info<struct tag_comment, std::string>; using errinfo_comment = boost::error_info<struct tag_comment, std::string>;
} }

View File

@ -17,6 +17,7 @@
#include <libdevcore/IpfsHash.h> #include <libdevcore/IpfsHash.h>
#include <libdevcore/Assertions.h>
#include <libdevcore/Exceptions.h> #include <libdevcore/Exceptions.h>
#include <libdevcore/picosha2.h> #include <libdevcore/picosha2.h>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
@ -55,11 +56,7 @@ string base58Encode(bytes const& _data)
bytes dev::ipfsHash(string _data) bytes dev::ipfsHash(string _data)
{ {
if (_data.length() >= 1024 * 256) assertThrow(_data.length() < 1024 * 256, DataTooLong, "IPFS hash for large (chunked) files not yet implemented.");
BOOST_THROW_EXCEPTION(
DataTooLong() <<
errinfo_comment("Ipfs hash for large (chunked) files not yet implemented.")
);
bytes lengthAsVarint = varintEncoding(_data.size()); bytes lengthAsVarint = varintEncoding(_data.size());

View File

@ -323,7 +323,7 @@ Json::Value Assembly::assemblyJSON(StringMap const& _sourceCodes) const
collection.append(createJsonValue("PUSH data", i.location().start, i.location().end, toStringInHex(i.data()))); collection.append(createJsonValue("PUSH data", i.location().start, i.location().end, toStringInHex(i.data())));
break; break;
default: default:
BOOST_THROW_EXCEPTION(InvalidOpcode()); assertThrow(false, InvalidOpcode, "");
} }
} }
@ -641,7 +641,7 @@ LinkerObject const& Assembly::assemble() const
ret.bytecode.push_back((uint8_t)Instruction::JUMPDEST); ret.bytecode.push_back((uint8_t)Instruction::JUMPDEST);
break; break;
default: default:
BOOST_THROW_EXCEPTION(InvalidOpcode()); assertThrow(false, InvalidOpcode, "Unexpected opcode while assembling.");
} }
} }

View File

@ -81,7 +81,7 @@ unsigned AssemblyItem::bytesRequired(unsigned _addressLength) const
default: default:
break; break;
} }
BOOST_THROW_EXCEPTION(InvalidOpcode()); assertThrow(false, InvalidOpcode, "");
} }
int AssemblyItem::arguments() const int AssemblyItem::arguments() const
@ -212,7 +212,7 @@ string AssemblyItem::toAssemblyText() const
assertThrow(false, AssemblyException, "Invalid assembly item."); assertThrow(false, AssemblyException, "Invalid assembly item.");
break; break;
default: default:
BOOST_THROW_EXCEPTION(InvalidOpcode()); assertThrow(false, InvalidOpcode, "");
} }
if (m_jumpType == JumpType::IntoFunction || m_jumpType == JumpType::OutOfFunction) if (m_jumpType == JumpType::IntoFunction || m_jumpType == JumpType::OutOfFunction)
{ {
@ -277,7 +277,7 @@ ostream& dev::eth::operator<<(ostream& _out, AssemblyItem const& _item)
_out << " ???"; _out << " ???";
break; break;
default: default:
BOOST_THROW_EXCEPTION(InvalidOpcode()); assertThrow(false, InvalidOpcode, "");
} }
return _out; return _out;
} }

View File

@ -158,11 +158,10 @@ AssemblyItems CSECodeGenerator::generateCode(
for (auto id: {p.first, p.second}) for (auto id: {p.first, p.second})
if (unsigned seqNr = m_expressionClasses.representative(id).sequenceNumber) if (unsigned seqNr = m_expressionClasses.representative(id).sequenceNumber)
{ {
if (seqNr < _initialSequenceNumber) // Invalid sequenced operation.
// Invalid sequenced operation. // @todo quick fix for now. Proper fix needs to choose representative with higher
// @todo quick fix for now. Proper fix needs to choose representative with higher // sequence number during dependency analysis.
// sequence number during dependency analysis. assertThrow(seqNr >= _initialSequenceNumber, StackTooDeepException, "");
BOOST_THROW_EXCEPTION(StackTooDeepException());
sequencedExpressions.insert(make_pair(seqNr, id)); sequencedExpressions.insert(make_pair(seqNr, id));
} }
@ -222,12 +221,9 @@ void CSECodeGenerator::addDependencies(Id _c)
return; // we already computed the dependencies for _c return; // we already computed the dependencies for _c
ExpressionClasses::Expression expr = m_expressionClasses.representative(_c); ExpressionClasses::Expression expr = m_expressionClasses.representative(_c);
assertThrow(expr.item, OptimizerException, ""); assertThrow(expr.item, OptimizerException, "");
if (expr.item->type() == UndefinedItem) // If this exception happens, we need to find a different way to generate the
BOOST_THROW_EXCEPTION( // compound expression.
// If this exception happens, we need to find a different way to generate the assertThrow(expr.item->type() != UndefinedItem, ItemNotAvailableException, "Undefined item requested but not available.");
// compound expression.
ItemNotAvailableException() << errinfo_comment("Undefined item requested but not available.")
);
for (Id argument: expr.arguments) for (Id argument: expr.arguments)
{ {
addDependencies(argument); addDependencies(argument);

View File

@ -33,5 +33,8 @@ struct OptimizerException: virtual AssemblyException {};
struct StackTooDeepException: virtual OptimizerException {}; struct StackTooDeepException: virtual OptimizerException {};
struct ItemNotAvailableException: virtual OptimizerException {}; struct ItemNotAvailableException: virtual OptimizerException {};
DEV_SIMPLE_EXCEPTION(InvalidDeposit);
DEV_SIMPLE_EXCEPTION(InvalidOpcode);
} }
} }

View File

@ -31,9 +31,6 @@ namespace dev
namespace eth namespace eth
{ {
DEV_SIMPLE_EXCEPTION(InvalidDeposit);
DEV_SIMPLE_EXCEPTION(InvalidOpcode);
/// Virtual machine bytecode instruction. /// Virtual machine bytecode instruction.
enum class Instruction: uint8_t enum class Instruction: uint8_t
{ {

View File

@ -207,11 +207,11 @@ void CodeGenerator::assemble(
} }
catch (StackTooDeepError const& _e) catch (StackTooDeepError const& _e)
{ {
BOOST_THROW_EXCEPTION( solAssert(
InternalCompilerError() << errinfo_comment( false,
"Stack too deep when compiling inline assembly" + "Stack too deep when compiling inline assembly" +
(_e.comment() ? ": " + *_e.comment() : ".") (_e.comment() ? ": " + *_e.comment() : ".")
)); );
} }
solAssert(transform.stackErrors().empty(), "Stack errors present but not thrown."); solAssert(transform.stackErrors().empty(), "Stack errors present but not thrown.");
} }