From c577e043a4d54ac740aab770a6af38d74a7a427d Mon Sep 17 00:00:00 2001 From: Leonardo Alt Date: Wed, 1 Aug 2018 19:01:50 +0200 Subject: Disallow structs in events without ABIEncoderV2 --- Changelog.md | 2 ++ libsolidity/analysis/TypeChecker.cpp | 10 ++++++++++ test/libsolidity/SolidityABIJSON.cpp | 1 + test/libsolidity/syntaxTests/emit/emit_empty.sol | 7 +++++++ test/libsolidity/syntaxTests/emit/emit_non_event.sol | 10 ++++++++++ test/libsolidity/syntaxTests/emit_non_event.sol | 10 ---------- test/libsolidity/syntaxTests/events/event_nested_array.sol | 5 +++++ test/libsolidity/syntaxTests/events/event_nested_array_2.sol | 4 ++++ .../syntaxTests/events/event_nested_array_in_struct.sol | 6 ++++++ test/libsolidity/syntaxTests/events/event_struct.sol | 6 ++++++ test/libsolidity/syntaxTests/events/event_struct_indexed.sol | 6 ++++++ 11 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 test/libsolidity/syntaxTests/emit/emit_empty.sol create mode 100644 test/libsolidity/syntaxTests/emit/emit_non_event.sol delete mode 100644 test/libsolidity/syntaxTests/emit_non_event.sol create mode 100644 test/libsolidity/syntaxTests/events/event_nested_array.sol create mode 100644 test/libsolidity/syntaxTests/events/event_nested_array_2.sol create mode 100644 test/libsolidity/syntaxTests/events/event_nested_array_in_struct.sol create mode 100644 test/libsolidity/syntaxTests/events/event_struct.sol create mode 100644 test/libsolidity/syntaxTests/events/event_struct_indexed.sol diff --git a/Changelog.md b/Changelog.md index 0ad1b863..26f7d5e2 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,7 @@ ### 0.4.25 (unreleased) +Bugfixes: + * Type Checker: Report error when using structs in events without experimental ABIEncoderV2. This used to crash or log the wrong values. ### 0.4.24 (2018-05-16) diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 30302908..f6980872 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -864,6 +864,7 @@ void TypeChecker::visitManually( bool TypeChecker::visit(EventDefinition const& _eventDef) { + solAssert(_eventDef.visibility() > Declaration::Visibility::Internal, ""); unsigned numIndexed = 0; for (ASTPointer const& var: _eventDef.parameters()) { @@ -873,6 +874,15 @@ bool TypeChecker::visit(EventDefinition const& _eventDef) m_errorReporter.typeError(var->location(), "Type is required to live outside storage."); if (!type(*var)->interfaceType(false)) m_errorReporter.typeError(var->location(), "Internal or recursive type is not allowed as event parameter type."); + if ( + !_eventDef.sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::ABIEncoderV2) && + !typeSupportedByOldABIEncoder(*type(*var)) + ) + m_errorReporter.typeError( + var->location(), + "This type is only supported in the new experimental ABI encoder. " + "Use \"pragma experimental ABIEncoderV2;\" to enable the feature." + ); } if (_eventDef.isAnonymous() && numIndexed > 4) m_errorReporter.typeError(_eventDef.location(), "More than 4 indexed arguments for anonymous event."); diff --git a/test/libsolidity/SolidityABIJSON.cpp b/test/libsolidity/SolidityABIJSON.cpp index 107abc26..3475b018 100644 --- a/test/libsolidity/SolidityABIJSON.cpp +++ b/test/libsolidity/SolidityABIJSON.cpp @@ -1036,6 +1036,7 @@ BOOST_AUTO_TEST_CASE(return_structs_with_contracts) BOOST_AUTO_TEST_CASE(event_structs) { char const* text = R"( + pragma experimental ABIEncoderV2; contract C { struct S { uint a; T[] sub; bytes b; } struct T { uint[2] x; } diff --git a/test/libsolidity/syntaxTests/emit/emit_empty.sol b/test/libsolidity/syntaxTests/emit/emit_empty.sol new file mode 100644 index 00000000..819d88fe --- /dev/null +++ b/test/libsolidity/syntaxTests/emit/emit_empty.sol @@ -0,0 +1,7 @@ +contract C { + function f() public { + emit; + } +} +// ---- +// ParserError: (45-46): Expected event name or path. diff --git a/test/libsolidity/syntaxTests/emit/emit_non_event.sol b/test/libsolidity/syntaxTests/emit/emit_non_event.sol new file mode 100644 index 00000000..1df6990d --- /dev/null +++ b/test/libsolidity/syntaxTests/emit/emit_non_event.sol @@ -0,0 +1,10 @@ +contract C { + uint256 Test; + + function f() { + emit Test(); + } +} +// ---- +// TypeError: (56-62): Type is not callable +// TypeError: (56-60): Expression has to be an event invocation. diff --git a/test/libsolidity/syntaxTests/emit_non_event.sol b/test/libsolidity/syntaxTests/emit_non_event.sol deleted file mode 100644 index 1df6990d..00000000 --- a/test/libsolidity/syntaxTests/emit_non_event.sol +++ /dev/null @@ -1,10 +0,0 @@ -contract C { - uint256 Test; - - function f() { - emit Test(); - } -} -// ---- -// TypeError: (56-62): Type is not callable -// TypeError: (56-60): Expression has to be an event invocation. diff --git a/test/libsolidity/syntaxTests/events/event_nested_array.sol b/test/libsolidity/syntaxTests/events/event_nested_array.sol new file mode 100644 index 00000000..70af63b6 --- /dev/null +++ b/test/libsolidity/syntaxTests/events/event_nested_array.sol @@ -0,0 +1,5 @@ +contract c { + event E(uint[][]); +} +// ---- +// TypeError: (25-33): This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature. diff --git a/test/libsolidity/syntaxTests/events/event_nested_array_2.sol b/test/libsolidity/syntaxTests/events/event_nested_array_2.sol new file mode 100644 index 00000000..5825650e --- /dev/null +++ b/test/libsolidity/syntaxTests/events/event_nested_array_2.sol @@ -0,0 +1,4 @@ +contract c { + event E(uint[2][]); +} +// ---- diff --git a/test/libsolidity/syntaxTests/events/event_nested_array_in_struct.sol b/test/libsolidity/syntaxTests/events/event_nested_array_in_struct.sol new file mode 100644 index 00000000..fd59e962 --- /dev/null +++ b/test/libsolidity/syntaxTests/events/event_nested_array_in_struct.sol @@ -0,0 +1,6 @@ +contract c { + struct S { uint x; uint[][] arr; } + event E(S); +} +// ---- +// TypeError: (61-62): This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature. diff --git a/test/libsolidity/syntaxTests/events/event_struct.sol b/test/libsolidity/syntaxTests/events/event_struct.sol new file mode 100644 index 00000000..c955dc5e --- /dev/null +++ b/test/libsolidity/syntaxTests/events/event_struct.sol @@ -0,0 +1,6 @@ +contract c { + struct S { uint a ; } + event E(S); +} +// ---- +// TypeError: (51-52): This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature. diff --git a/test/libsolidity/syntaxTests/events/event_struct_indexed.sol b/test/libsolidity/syntaxTests/events/event_struct_indexed.sol new file mode 100644 index 00000000..69ee5017 --- /dev/null +++ b/test/libsolidity/syntaxTests/events/event_struct_indexed.sol @@ -0,0 +1,6 @@ +contract c { + struct S { uint a ; } + event E(S indexed); +} +// ---- +// TypeError: (51-52): This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature. -- cgit v1.2.3