mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[Sol->Yul] Adding bounds check when increasing size of byte array.
This commit is contained in:
parent
2a4a2d8f7b
commit
3a4233f3ad
@ -1209,13 +1209,9 @@ std::string YulUtilFunctions::resizeArrayFunction(ArrayType const& _type)
|
|||||||
string YulUtilFunctions::resizeDynamicByteArrayFunction(ArrayType const& _type)
|
string YulUtilFunctions::resizeDynamicByteArrayFunction(ArrayType const& _type)
|
||||||
{
|
{
|
||||||
string functionName = "resize_array_" + _type.identifier();
|
string functionName = "resize_array_" + _type.identifier();
|
||||||
return m_functionCollector.createFunction(functionName, [&]() {
|
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>&) {
|
||||||
|
_args = {"array", "newLen"};
|
||||||
return Whiskers(R"(
|
return Whiskers(R"(
|
||||||
function <functionName>(array, newLen) {
|
|
||||||
if gt(newLen, <maxArrayLength>) {
|
|
||||||
<panic>()
|
|
||||||
}
|
|
||||||
|
|
||||||
let data := sload(array)
|
let data := sload(array)
|
||||||
let oldLen := <extractLength>(data)
|
let oldLen := <extractLength>(data)
|
||||||
|
|
||||||
@ -1226,11 +1222,8 @@ string YulUtilFunctions::resizeDynamicByteArrayFunction(ArrayType const& _type)
|
|||||||
if lt(newLen, oldLen) {
|
if lt(newLen, oldLen) {
|
||||||
<decreaseSize>(array, data, oldLen, newLen)
|
<decreaseSize>(array, data, oldLen, newLen)
|
||||||
}
|
}
|
||||||
})")
|
)")
|
||||||
("functionName", functionName)
|
|
||||||
("panic", panicFunction(PanicCode::ResourceError))
|
|
||||||
("extractLength", extractByteArrayLengthFunction())
|
("extractLength", extractByteArrayLengthFunction())
|
||||||
("maxArrayLength", (u256(1) << 64).str())
|
|
||||||
("decreaseSize", decreaseByteArraySizeFunction(_type))
|
("decreaseSize", decreaseByteArraySizeFunction(_type))
|
||||||
("increaseSize", increaseByteArraySizeFunction(_type))
|
("increaseSize", increaseByteArraySizeFunction(_type))
|
||||||
.render();
|
.render();
|
||||||
@ -1282,9 +1275,11 @@ string YulUtilFunctions::decreaseByteArraySizeFunction(ArrayType const& _type)
|
|||||||
string YulUtilFunctions::increaseByteArraySizeFunction(ArrayType const& _type)
|
string YulUtilFunctions::increaseByteArraySizeFunction(ArrayType const& _type)
|
||||||
{
|
{
|
||||||
string functionName = "byte_array_increase_size_" + _type.identifier();
|
string functionName = "byte_array_increase_size_" + _type.identifier();
|
||||||
return m_functionCollector.createFunction(functionName, [&]() {
|
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>&) {
|
||||||
|
_args = {"array", "data", "oldLen", "newLen"};
|
||||||
return Whiskers(R"(
|
return Whiskers(R"(
|
||||||
function <functionName>(array, data, oldLen, newLen) {
|
if gt(newLen, <maxArrayLength>) { <panic>() }
|
||||||
|
|
||||||
switch lt(oldLen, 32)
|
switch lt(oldLen, 32)
|
||||||
case 0 {
|
case 0 {
|
||||||
// in this case array stays unpacked, so we just set new length
|
// in this case array stays unpacked, so we just set new length
|
||||||
@ -1303,8 +1298,9 @@ string YulUtilFunctions::increaseByteArraySizeFunction(ArrayType const& _type)
|
|||||||
sstore(array, <encodeUsedSetLen>(data, newLen))
|
sstore(array, <encodeUsedSetLen>(data, newLen))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})")
|
)")
|
||||||
("functionName", functionName)
|
("panic", panicFunction(PanicCode::ResourceError))
|
||||||
|
("maxArrayLength", (u256(1) << 64).str())
|
||||||
("dataPosition", arrayDataAreaFunction(_type))
|
("dataPosition", arrayDataAreaFunction(_type))
|
||||||
("encodeUsedSetLen", shortByteArrayEncodeUsedAreaSetLengthFunction())
|
("encodeUsedSetLen", shortByteArrayEncodeUsedAreaSetLengthFunction())
|
||||||
.render();
|
.render();
|
||||||
|
@ -23,7 +23,7 @@ contract C {
|
|||||||
// ----
|
// ----
|
||||||
// l() -> 0
|
// l() -> 0
|
||||||
// g(uint256): 70 ->
|
// g(uint256): 70 ->
|
||||||
// gas irOptimized: 429679
|
// gas irOptimized: 434229
|
||||||
// gas legacy: 419791
|
// gas legacy: 419791
|
||||||
// gas legacyOptimized: 415408
|
// gas legacyOptimized: 415408
|
||||||
// l() -> 70
|
// l() -> 70
|
||||||
|
Loading…
Reference in New Issue
Block a user