Makes array length access read-only.

This commit is contained in:
Erik Kundt
2019-11-19 16:09:21 +01:00
parent 94272d44aa
commit ecaed1030f
9 changed files with 13 additions and 143 deletions
+4 -22
View File
@@ -2398,14 +2398,7 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
if (auto const* structType = dynamic_cast<StructType const*>(exprType))
annotation.isLValue = !structType->dataStoredIn(DataLocation::CallData);
else if (exprType->category() == Type::Category::Array)
{
auto const& arrayType(dynamic_cast<ArrayType const&>(*exprType));
annotation.isLValue = (
memberName == "length" &&
arrayType.location() == DataLocation::Storage &&
arrayType.isDynamicallySized()
);
}
annotation.isLValue = false;
else if (exprType->category() == Type::Category::FixedBytes)
annotation.isLValue = false;
else if (TypeType const* typeType = dynamic_cast<decltype(typeType)>(exprType))
@@ -2862,20 +2855,9 @@ void TypeChecker::requireLValue(Expression const& _expression)
if (structType->dataStoredIn(DataLocation::CallData))
return "Calldata structs are read-only.";
}
else if (auto arrayType = dynamic_cast<ArrayType const*>(type(memberAccess->expression())))
if (
memberAccess->memberName() == "length" ||
memberAccess->memberName() == "push"
)
switch (arrayType->location())
{
case DataLocation::Memory:
return "Memory arrays cannot be resized.";
case DataLocation::CallData:
return "Calldata arrays cannot be resized.";
case DataLocation::Storage:
break;
}
else if (dynamic_cast<ArrayType const*>(type(memberAccess->expression())))
if (memberAccess->memberName() == "length")
return "Member \"length\" is read-only and cannot be used to resize arrays.";
}
if (auto identifier = dynamic_cast<Identifier const*>(&_expression))
+1 -1
View File
@@ -381,7 +381,7 @@ void ViewPureChecker::endVisit(MemberAccess const& _memberAccess)
{
auto const& type = dynamic_cast<ArrayType const&>(*_memberAccess.expression().annotation().type);
if (member == "length" && type.isDynamicallySized() && type.dataStoredIn(DataLocation::Storage))
mutability = writes ? StateMutability::NonPayable : StateMutability::View;
mutability = StateMutability::View;
break;
}
default: