From 2d0daae7968f5237231e50173a391652eca75674 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Thu, 21 Feb 2019 16:38:02 +0100 Subject: [PATCH] Disallows index access on contracts and libraries. --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 2 ++ .../syntaxTests/array/invalid/contract_index_access.sol | 7 +++++++ .../syntaxTests/array/invalid/library_index_access.sol | 7 +++++++ 4 files changed, 17 insertions(+) create mode 100644 test/libsolidity/syntaxTests/array/invalid/contract_index_access.sol create mode 100644 test/libsolidity/syntaxTests/array/invalid/library_index_access.sol diff --git a/Changelog.md b/Changelog.md index fed2c953f..752808afc 100644 --- a/Changelog.md +++ b/Changelog.md @@ -19,6 +19,7 @@ Bugfixes: * Inline Assembly: Proper error message for missing variables. * Optimizer: Fix internal error related to unused tag removal across assemblies. This never generated any invalid code. * SMTChecker: Fixed crash when used with fixed-sized arrays. + * TypeChecker: Fix internal error and disallow index access on contracts and libraries. * Yul: Properly detect name clashes with functions before their declaration. * Yul: Take builtin functions into account in the compilability checker. diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 0208fc7ac..a431930f6 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -2208,6 +2208,8 @@ bool TypeChecker::visit(IndexAccess const& _access) case Type::Category::TypeType: { TypeType const& typeType = dynamic_cast(*baseType); + if (dynamic_cast(typeType.actualType().get())) + m_errorReporter.typeError(_access.location(), "Index access for contracts or libraries is not possible."); if (!index) resultType = make_shared(make_shared(DataLocation::Memory, typeType.actualType())); else diff --git a/test/libsolidity/syntaxTests/array/invalid/contract_index_access.sol b/test/libsolidity/syntaxTests/array/invalid/contract_index_access.sol new file mode 100644 index 000000000..9f79f3018 --- /dev/null +++ b/test/libsolidity/syntaxTests/array/invalid/contract_index_access.sol @@ -0,0 +1,7 @@ +contract C { + function f() view public { + C[0]; + } +} +// ---- +// TypeError: (52-56): Index access for contracts or libraries is not possible. diff --git a/test/libsolidity/syntaxTests/array/invalid/library_index_access.sol b/test/libsolidity/syntaxTests/array/invalid/library_index_access.sol new file mode 100644 index 000000000..442150c97 --- /dev/null +++ b/test/libsolidity/syntaxTests/array/invalid/library_index_access.sol @@ -0,0 +1,7 @@ +library C { + function f() view public { + C[0]; + } +} +// ---- +// TypeError: (51-55): Index access for contracts or libraries is not possible.