mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Use revert for out-of-bounds array index access in getter.
This commit is contained in:
@@ -170,7 +170,16 @@ void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const&
|
||||
// pop offset
|
||||
m_context << Instruction::POP;
|
||||
utils().copyToStackTop(paramTypes.size() - i + 1, 1);
|
||||
ArrayUtils(m_context).accessIndex(*arrayType);
|
||||
|
||||
ArrayUtils(m_context).retrieveLength(*arrayType, 1);
|
||||
// Stack: ref [length] index length
|
||||
// check out-of-bounds access
|
||||
m_context << Instruction::DUP2 << Instruction::LT;
|
||||
auto tag = m_context.appendConditionalJump();
|
||||
m_context << u256(0) << Instruction::DUP1 << Instruction::REVERT;
|
||||
m_context << tag;
|
||||
|
||||
ArrayUtils(m_context).accessIndex(*arrayType, false);
|
||||
returnType = arrayType->baseType();
|
||||
}
|
||||
else
|
||||
|
||||
@@ -348,18 +348,25 @@ string IRGenerator::generateGetter(VariableDeclaration const& _varDecl)
|
||||
mappingType ? *mappingType->keyType() : *TypeProvider::uint256()
|
||||
).stackSlots();
|
||||
parameters += keys;
|
||||
code += Whiskers(R"(
|
||||
|
||||
Whiskers templ(R"(
|
||||
<?array>
|
||||
if iszero(lt(<keys>, <length>(slot))) { revert(0, 0) }
|
||||
</array>
|
||||
slot<?array>, offset</array> := <indexAccess>(slot<?+keys>, <keys></+keys>)
|
||||
)")
|
||||
(
|
||||
)");
|
||||
templ(
|
||||
"indexAccess",
|
||||
mappingType ?
|
||||
m_utils.mappingIndexAccessFunction(*mappingType, *mappingType->keyType()) :
|
||||
m_utils.storageArrayIndexAccessFunction(*arrayType)
|
||||
)
|
||||
("array", arrayType != nullptr)
|
||||
("keys", joinHumanReadable(keys))
|
||||
.render();
|
||||
("keys", joinHumanReadable(keys));
|
||||
if (arrayType)
|
||||
templ("length", m_utils.arrayLengthFunction(*arrayType));
|
||||
|
||||
code += templ.render();
|
||||
|
||||
currentType = mappingType ? mappingType->valueType() : arrayType->baseType();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user