Skip to content

Commit

Permalink
fix(common): fix memory corruption for dynamic to static array conver…
Browse files Browse the repository at this point in the history
…sion (#1598)

Co-authored-by: alvarius <alvarius@lattice.xyz>
  • Loading branch information
dk1a and alvrs committed Sep 25, 2023
1 parent 5741d53 commit c4f4924
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .changeset/violet-insects-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@latticexyz/cli": patch
"@latticexyz/common": patch
---

Table libraries now correctly handle uninitialized fixed length arrays.
60 changes: 45 additions & 15 deletions packages/cli/contracts/src/codegen/tables/Dynamics1.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions packages/cli/contracts/src/codegen/tables/Singleton.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions packages/common/src/codegen/render-solidity/renderTypeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ function renderWrapperStaticArray(
) pure returns (
${elementType}[${staticLength}] memory _result
) {
// in memory static arrays are just dynamic arrays without the length byte
assembly {
_result := add(_value, 0x20)
if (_value.length < ${staticLength}) {
// return an uninitialized array if the length is smaller than the fixed length to avoid memory corruption
return _result;
} else {
// in memory static arrays are just dynamic arrays without the length byte
// (without the length check this could lead to memory corruption)
assembly {
_result := add(_value, 0x20)
}
}
}
`;
Expand Down

0 comments on commit c4f4924

Please sign in to comment.