From c622a1e56c0a02b890b45fd15f1fb4cb9d119b3b Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 18 Jul 2018 17:20:26 +0200 Subject: [PATCH] Enforces data location of local mappings to storage. --- Changelog.md | 1 + libsolidity/analysis/ReferencesResolver.cpp | 9 ++++++++- test/libsolidity/SolidityEndToEndTest.cpp | 2 +- .../nameAndTypeResolution/016_assignment_to_mapping.sol | 4 ++-- .../025_comparison_of_mapping_types.sol | 4 ++-- .../210_uninitialized_mapping_variable.sol | 4 ++-- .../264_mapping_in_memory_array.sol | 4 ++-- .../288_conditional_with_all_types.sol | 2 +- .../types/mapping/mapping_data_location_calldata.sol | 9 +++++++++ .../types/mapping/mapping_data_location_default.sol | 9 +++++++++ .../types/mapping/mapping_data_location_memory.sol | 9 +++++++++ 11 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 test/libsolidity/syntaxTests/types/mapping/mapping_data_location_calldata.sol create mode 100644 test/libsolidity/syntaxTests/types/mapping/mapping_data_location_default.sol create mode 100644 test/libsolidity/syntaxTests/types/mapping/mapping_data_location_memory.sol diff --git a/Changelog.md b/Changelog.md index 1064d8f67..a1e8a0948 100644 --- a/Changelog.md +++ b/Changelog.md @@ -69,6 +69,7 @@ Bugfixes: * Tests: Fix chain parameters to make ipc tests work with newer versions of cpp-ethereum. * Code Generator: Fix allocation of byte arrays (zeroed out too much memory). * Fix NatSpec json output for `@notice` and `@dev` tags on contract definitions. + * References Resolver: Enforce ``storage`` as data location for mappings. * Type Checker: Consider fixed size arrays when checking for recursive structs. * Type System: Allow arbitrary exponents for literals with a mantissa of zero. diff --git a/libsolidity/analysis/ReferencesResolver.cpp b/libsolidity/analysis/ReferencesResolver.cpp index dfcbf8887..5815e3d20 100644 --- a/libsolidity/analysis/ReferencesResolver.cpp +++ b/libsolidity/analysis/ReferencesResolver.cpp @@ -404,9 +404,16 @@ void ReferencesResolver::endVisit(VariableDeclaration const& _variable) } isPointer = !_variable.isStateVariable(); } - type = ref->copyForLocation(typeLoc, isPointer); } + else if (dynamic_cast(type.get())) + { + if (_variable.isLocalVariable() && varLoc != Location::Storage) + typeError( + _variable.location(), + "Data location for mappings must be specified as \"storage\"." + ); + } else if (varLoc != Location::Default && !ref) typeError(_variable.location(), "Data location can only be given for array or struct types."); diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index d8f2f531e..b9114b277 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1534,7 +1534,7 @@ BOOST_AUTO_TEST_CASE(struct_reference) } function set() public { data.z = 2; - mapping(uint8 => s2) map = data.recursive; + mapping(uint8 => s2) storage map = data.recursive; s2 storage inner = map[0]; inner.z = 3; inner.recursive[0].z = inner.recursive[1].z + 1; diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/016_assignment_to_mapping.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/016_assignment_to_mapping.sol index 8bf45c3f7..27b1ea969 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/016_assignment_to_mapping.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/016_assignment_to_mapping.sol @@ -4,9 +4,9 @@ contract test { } str data; function fun() public { - mapping(uint=>uint) a = data.map; + mapping(uint=>uint) storage a = data.map; data.map = a; } } // ---- -// TypeError: (164-176): Mappings cannot be assigned to. +// TypeError: (172-184): Mappings cannot be assigned to. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/025_comparison_of_mapping_types.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/025_comparison_of_mapping_types.sol index 27651d632..b15666c0b 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/025_comparison_of_mapping_types.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/025_comparison_of_mapping_types.sol @@ -1,9 +1,9 @@ contract C { mapping(uint => uint) x; function f() public returns (bool ret) { - mapping(uint => uint) y = x; + mapping(uint => uint) storage y = x; return x == y; } } // ---- -// TypeError: (139-145): Operator == not compatible with types mapping(uint256 => uint256) and mapping(uint256 => uint256) +// TypeError: (147-153): Operator == not compatible with types mapping(uint256 => uint256) and mapping(uint256 => uint256) diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/210_uninitialized_mapping_variable.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/210_uninitialized_mapping_variable.sol index 6b25cdfee..0547ace1d 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/210_uninitialized_mapping_variable.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/210_uninitialized_mapping_variable.sol @@ -1,8 +1,8 @@ contract C { function f() public { - mapping(uint => uint) x; + mapping(uint => uint) storage x; x; } } // ---- -// TypeError: (47-70): Uninitialized mapping. Mappings cannot be created dynamically, you have to assign them from a state variable. +// TypeError: (47-78): Uninitialized mapping. Mappings cannot be created dynamically, you have to assign them from a state variable. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/264_mapping_in_memory_array.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/264_mapping_in_memory_array.sol index f0bb557bd..e45e09de8 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/264_mapping_in_memory_array.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/264_mapping_in_memory_array.sol @@ -1,7 +1,7 @@ contract C { function f(uint size) public { - mapping(uint => uint) x = new mapping(uint => uint)[](4); + mapping(uint => uint) storage x = new mapping(uint => uint)[](4); } } // ---- -// TypeError: (86-109): Type cannot live outside storage. +// TypeError: (94-117): Type cannot live outside storage. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/288_conditional_with_all_types.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/288_conditional_with_all_types.sol index 41e72d606..e9ab08bae 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/288_conditional_with_all_types.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/288_conditional_with_all_types.sol @@ -71,7 +71,7 @@ contract C { (uint n, uint o) = true ? (1, 2) : (3, 4); (n, o) = (o, n); // Avoid unused var warning // mapping - mapping(uint8 => uint8) p = true ? table1 : table2; + mapping(uint8 => uint8) storage p = true ? table1 : table2; p[0] = 0; // Avoid unused var warning // typetype uint32 q = true ? uint32(1) : uint32(2); diff --git a/test/libsolidity/syntaxTests/types/mapping/mapping_data_location_calldata.sol b/test/libsolidity/syntaxTests/types/mapping/mapping_data_location_calldata.sol new file mode 100644 index 000000000..c73c7f32b --- /dev/null +++ b/test/libsolidity/syntaxTests/types/mapping/mapping_data_location_calldata.sol @@ -0,0 +1,9 @@ +contract c { + mapping(uint => uint) y; + function f() view public { + mapping(uint => uint) calldata x = y; + x; + } +} +// ---- +// TypeError: (81-113): Data location for mappings must be specified as "storage". diff --git a/test/libsolidity/syntaxTests/types/mapping/mapping_data_location_default.sol b/test/libsolidity/syntaxTests/types/mapping/mapping_data_location_default.sol new file mode 100644 index 000000000..85531ae1f --- /dev/null +++ b/test/libsolidity/syntaxTests/types/mapping/mapping_data_location_default.sol @@ -0,0 +1,9 @@ +contract c { + mapping(uint => uint) y; + function f() view public { + mapping(uint => uint) x = y; + x; + } +} +// ---- +// TypeError: (81-104): Data location for mappings must be specified as "storage". diff --git a/test/libsolidity/syntaxTests/types/mapping/mapping_data_location_memory.sol b/test/libsolidity/syntaxTests/types/mapping/mapping_data_location_memory.sol new file mode 100644 index 000000000..7151e8876 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/mapping/mapping_data_location_memory.sol @@ -0,0 +1,9 @@ +contract c { + mapping(uint => uint) y; + function f() view public { + mapping(uint => uint) memory x = y; + x; + } +} +// ---- +// TypeError: (81-111): Data location for mappings must be specified as "storage".