Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Disallow structs in getters for old encoder. #5526

Merged
merged 3 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Compiler Features:
Bugfixes:
* Assembly output: Do not mix in/out jump annotations with arguments.
* Code Generator: Annotate jump from calldata decoder to function as "jump in".
* Type Checker: Properly detect different return types when overriding an external interface function with a public contract function.
* Optimizer: Fix nondeterminism bug related to the boost version and constants representation. The bug only resulted in less optimal but still correct code because the generated routine is always verified to be correct.
* Type Checker: Properly detect different return types when overriding an external interface function with a public contract function.
* Type Checker: Disallow struct return types for getters of public state variables unless the new ABI encoder is active.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code seems to allow simple structs though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, need to think whether this can be rephrased, but it's hard. If the resulting type is only a struct, then it is broken up and the getter actually returns multiple values instead of a struct.


Build System:
* Emscripten: Upgrade to Emscripten SDK 1.37.21 and boost 1.67.
Expand Down
41 changes: 31 additions & 10 deletions libsolidity/analysis/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@
*/

#include <libsolidity/analysis/TypeChecker.h>
#include <memory>
#include <boost/algorithm/cxx11/all_of.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <libsolidity/ast/AST.h>

#include <libyul/AsmAnalysis.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmData.h>

#include <liblangutil/ErrorReporter.h>

#include <libdevcore/Algorithms.h>
#include <libdevcore/StringUtils.h>

#include <boost/algorithm/cxx11/all_of.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/range/adaptor/reversed.hpp>

#include <memory>
#include <vector>

using namespace std;
using namespace dev;
Expand Down Expand Up @@ -814,11 +821,25 @@ bool TypeChecker::visit(VariableDeclaration const& _variable)
if (!varType->canLiveOutsideStorage())
m_errorReporter.typeError(_variable.location(), "Type " + varType->toString() + " is only valid in storage.");
}
else if (
_variable.visibility() >= VariableDeclaration::Visibility::Public &&
!FunctionType(_variable).interfaceFunctionType()
)
m_errorReporter.typeError(_variable.location(), "Internal or recursive type is not allowed for public state variables.");
else if (_variable.visibility() >= VariableDeclaration::Visibility::Public)
{
FunctionType getter(_variable);
if (!_variable.sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::ABIEncoderV2))
{
vector<string> unsupportedTypes;
for (auto const& param: getter.parameterTypes() + getter.returnParameterTypes())
if (!typeSupportedByOldABIEncoder(*param))
unsupportedTypes.emplace_back(param->toString());
if (!unsupportedTypes.empty())
m_errorReporter.typeError(_variable.location(),
"The following types are only supported for getters in the new experimental ABI encoder: " +
joinHumanReadable(unsupportedTypes) +
". Either remove \"public\" or use \"pragma experimental ABIEncoderV2;\" to enable the feature."
);
}
if (!getter.interfaceFunctionType())
m_errorReporter.typeError(_variable.location(), "Internal or recursive type is not allowed for public state variables.");
erak marked this conversation as resolved.
Show resolved Hide resolved
}

switch (varType->category())
{
Expand Down
7 changes: 7 additions & 0 deletions test/libsolidity/syntaxTests/getter/complex_struct.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract C {
struct Y {
uint a;
uint b;
}
mapping(uint256 => Y) public m;
}
11 changes: 11 additions & 0 deletions test/libsolidity/syntaxTests/getter/nested_structs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract C {
struct Y {
uint b;
}
struct X {
Y a;
}
mapping(uint256 => X) public m;
}
// ----
// TypeError: (88-118): The following types are only supported for getters in the new experimental ABI encoder: struct C.Y memory. Either remove "public" or use "pragma experimental ABIEncoderV2;" to enable the feature.
8 changes: 8 additions & 0 deletions test/libsolidity/syntaxTests/getter/recursive_struct.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
contract C {
struct Y {
Y[] x;
}
mapping(uint256 => Y) public m;
}
// ----
// TypeError: (53-83): Internal or recursive type is not allowed for public state variables.
6 changes: 6 additions & 0 deletions test/libsolidity/syntaxTests/getter/simple_struct.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
contract C {
struct Y {
uint b;
}
mapping(uint256 => Y) public m;
}