mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
added compile time check for out of bounds access for ordinary arrays
todo: check for dynamicaly sized arrays Conflicts: libsolidity/ExpressionCompiler.cpp
This commit is contained in:
parent
626a57826c
commit
b7b16b153b
@ -821,6 +821,8 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
|||||||
_indexAccess.baseExpression().accept(*this);
|
_indexAccess.baseExpression().accept(*this);
|
||||||
|
|
||||||
Type const& baseType = *_indexAccess.baseExpression().type();
|
Type const& baseType = *_indexAccess.baseExpression().type();
|
||||||
|
Type const& indexType = *_indexAccess.indexExpression()->type();
|
||||||
|
|
||||||
if (baseType.category() == Type::Category::Mapping)
|
if (baseType.category() == Type::Category::Mapping)
|
||||||
{
|
{
|
||||||
// stack: storage_base_ref
|
// stack: storage_base_ref
|
||||||
@ -861,8 +863,20 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
|
|||||||
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
|
solAssert(_indexAccess.indexExpression(), "Index expression expected.");
|
||||||
|
|
||||||
_indexAccess.indexExpression()->accept(*this);
|
_indexAccess.indexExpression()->accept(*this);
|
||||||
|
|
||||||
|
// check for dynamically sized arrays should be done after memberAccess visit to have length
|
||||||
|
if (
|
||||||
|
(indexType.category() == Type::Category::IntegerConstant) &&
|
||||||
|
((arrayType.isDynamicallySized() && arrayType.length()) || !arrayType.isDynamicallySized()))
|
||||||
|
{
|
||||||
|
IntegerConstantType const& constant = dynamic_cast<IntegerConstantType const&>(indexType);
|
||||||
|
if (arrayType.length() < constant.literalValue(nullptr))
|
||||||
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Out of bounds access."));
|
||||||
|
}
|
||||||
|
|
||||||
// stack layout: <base_ref> [<length>] <index>
|
// stack layout: <base_ref> [<length>] <index>
|
||||||
ArrayUtils(m_context).accessIndex(arrayType);
|
ArrayUtils(m_context).accessIndex(arrayType);
|
||||||
|
|
||||||
switch (arrayType.location())
|
switch (arrayType.location())
|
||||||
{
|
{
|
||||||
case DataLocation::Storage:
|
case DataLocation::Storage:
|
||||||
|
Loading…
Reference in New Issue
Block a user