From 99f4eec152aa724e1fb4500e514ec0b13bdb3bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rakhmad=20aulad=20alie=20=E2=80=94=20Nexus=20Gateway=20Arc?= =?UTF-8?q?hitect?= Date: Tue, 20 Oct 2026 15:26:23 +0700 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=9E=20Fix=20LibBytes.split=20null?= =?UTF-8?q?=20path=20and=20dynamicStructInCalldata=20bounds=20check=20(#15?= =?UTF-8?q?62)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/LibBytes.sol | 12 ++++- test/LibBytesEdgeCases.t.sol | 88 ++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 test/LibBytesEdgeCases.t.sol diff --git a/src/utils/LibBytes.sol b/src/utils/LibBytes.sol index f413b68b8c..1dd8e75e6f 100644 --- a/src/utils/LibBytes.sol +++ b/src/utils/LibBytes.sol @@ -546,6 +546,15 @@ library LibBytes { /// @solidity memory-safe-assembly assembly { let searchLen := mload(needle) + if gt(searchLen, mload(subject)) { + // Allocate a proper empty array instead of returning a null pointer, + // so that downstream consumers (e.g. `split`) never read the + // scratch space at 0x00 to determine the array length. + // We allocate one more word, so this array can be recycled for {split}. + result := mload(0x40) + mstore(result, 0) + mstore(0x40, add(result, 0x40)) + } if iszero(gt(searchLen, mload(subject))) { result := mload(0x40) let i := add(subject, 0x20) @@ -814,7 +823,8 @@ library LibBytes { let s := calldataload(add(a.offset, offset)) // Relative offset of `result` from `a.offset`. result.offset := add(a.offset, s) result.length := sub(a.length, s) - if or(shr(64, or(s, or(l, a.offset))), gt(offset, l)) { revert(l, 0x00) } + // forgefmt: disable-next-item + if or(shr(64, or(s, or(l, a.offset))), or(gt(offset, l), gt(s, l))) { revert(l, 0x00) } } } diff --git a/test/LibBytesEdgeCases.t.sol b/test/LibBytesEdgeCases.t.sol new file mode 100644 index 0000000000..4284804535 --- /dev/null +++ b/test/LibBytesEdgeCases.t.sol @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.4; + +import "./utils/forge-std/Test.sol"; +import {LibBytes} from "../src/utils/LibBytes.sol"; +import {EfficientHashLib} from "../src/utils/EfficientHashLib.sol"; + +/// @dev Tests for LibBytes edge cases: +/// - `split` when the delimiter is longer than the subject, +/// - `dynamicStructInCalldata` with a malformed struct offset. +contract LibBytesEdgeCasesTest is Test { + /// ------------------------------------------------------------------ + /// `split` with delimiter longer than subject. + /// Previously, `indicesOf` returned a null pointer on this path, and + /// `split` read the array length from the scratch space at 0x00, + /// which is not guaranteed to be zero. + /// ------------------------------------------------------------------ + /// Clean-scratch sanity: the null path works when scratch is zero. + function testSplitNullPathCleanScratch() public pure { + bytes[] memory p = LibBytes.split("ab", "xyz"); + require(p.length == 1, "length"); + require(bytes(p[0]).length == 2, "element length"); + } + + /// Scratch dirtied by a prior call to another library function. + /// Before the fix: corrupted result or OOG. After the fix: correct result. + function testSplitNullPathAfterHash() public { + bytes32 h = EfficientHashLib.hash(uint256(1)); + h; // silence unused + bytes[] memory p = LibBytes.split("ab", "xyz"); + assertEq(p.length, 1, "array length must be 1"); + assertEq(bytes(p[0]), "ab", "element must equal subject"); + } + + /// Two consecutive null-path splits: the first split itself leaves + /// scratch dirty for the second. Before the fix: OOG revert. + function testSplitNullPathTwoConsecutive() public pure { + bytes[] memory a = LibBytes.split("ab", "xyz"); + require(a.length == 1, "first split length"); + require(bytes(a[0]).length == 2, "first split element"); + bytes[] memory b = LibBytes.split("cd", "wxyz"); + require(b.length == 1, "second split length"); + require(bytes(b[0]).length == 2, "second split element"); + } + + /// ------------------------------------------------------------------ + /// `dynamicStructInCalldata` with a malformed struct offset. + /// Previously, a struct offset `s > a.length` was not rejected, and + /// `result.length = a.length - s` wrapped around to a huge value. + /// ------------------------------------------------------------------ + + Harness harness; + + function setUp() public { + harness = new Harness(); + } + + /// Malformed struct offset: a = 64 bytes, first word = 65 (> a.length). + /// Before the fix: no revert from LibBytes, returned slice had + /// length 2**256 - 1. After the fix: reverts with an empty reason + /// (the assembly `revert(l, 0x00)` bounds check). + function testDynamicStructInCalldataMalformedOffset() public { + bytes memory payload = abi.encode(uint256(65), uint256(0)); // 64 bytes, s = 65 + (bool ok, bytes memory reason) = + address(harness).call(abi.encodeWithSelector(Harness.f.selector, payload)); + assertFalse(ok, "must revert on out-of-bounds struct offset"); + assertEq(reason, bytes(""), "revert must come from LibBytes bounds check"); + } + + /// Valid input sanity: must NOT revert. + function testDynamicStructInCalldataValid() public view { + bytes memory payload = abi.encode(uint256(0x20), uint256(123)); + harness.f(payload); + } +} + +contract Harness { + /// Re-encodes the returned slice so the wrapped length is actually consumed. + function f(bytes calldata a) external pure returns (bytes calldata) { + bytes calldata s = LibBytes.dynamicStructInCalldata(a, 0); + // Touch the slice: reading its length forces use of the wrapped value. + require( + s.length < 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, "wrapped" + ); + return s; + } +} From cddcd0a8864f5828a39f048c50a86b2d62ae37a6 Mon Sep 17 00:00:00 2001 From: atarpara Date: Tue, 20 Oct 2026 13:58:03 +0530 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Optimize=20LibBytes.in?= =?UTF-8?q?dicesOf=20null=20path=20allocation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/LibBytes.sol | 21 ++++++--------------- src/utils/g/LibBytes.sol | 15 ++++++++------- test/LibString.t.sol | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/utils/LibBytes.sol b/src/utils/LibBytes.sol index 1dd8e75e6f..839f0f129d 100644 --- a/src/utils/LibBytes.sol +++ b/src/utils/LibBytes.sol @@ -545,20 +545,11 @@ library LibBytes { { /// @solidity memory-safe-assembly assembly { + result := mload(0x40) + let o := add(result, 0x20) let searchLen := mload(needle) - if gt(searchLen, mload(subject)) { - // Allocate a proper empty array instead of returning a null pointer, - // so that downstream consumers (e.g. `split`) never read the - // scratch space at 0x00 to determine the array length. - // We allocate one more word, so this array can be recycled for {split}. - result := mload(0x40) - mstore(result, 0) - mstore(0x40, add(result, 0x40)) - } if iszero(gt(searchLen, mload(subject))) { - result := mload(0x40) let i := add(subject, 0x20) - let o := add(result, 0x20) let subjectSearchEnd := add(sub(add(i, mload(subject)), searchLen), 1) let h := 0 // The hash of `needle`. if iszero(lt(searchLen, 0x20)) { @@ -587,11 +578,11 @@ library LibBytes { i := add(i, 1) if iszero(lt(i, subjectSearchEnd)) { break } } - mstore(result, shr(5, sub(o, add(result, 0x20)))) // Store the length of `result`. - // Allocate memory for result. - // We allocate one more word, so this array can be recycled for {split}. - mstore(0x40, add(o, 0x20)) } + mstore(result, shr(5, sub(o, add(result, 0x20)))) // Store the length of `result`. + // Allocate memory for result. + // We allocate one more word, so this array can be recycled for {split}. + mstore(0x40, add(o, 0x20)) } } diff --git a/src/utils/g/LibBytes.sol b/src/utils/g/LibBytes.sol index bbb688ce2c..589bd073fe 100644 --- a/src/utils/g/LibBytes.sol +++ b/src/utils/g/LibBytes.sol @@ -549,11 +549,11 @@ library LibBytes { { /// @solidity memory-safe-assembly assembly { + result := mload(0x40) + let o := add(result, 0x20) let searchLen := mload(needle) if iszero(gt(searchLen, mload(subject))) { - result := mload(0x40) let i := add(subject, 0x20) - let o := add(result, 0x20) let subjectSearchEnd := add(sub(add(i, mload(subject)), searchLen), 1) let h := 0 // The hash of `needle`. if iszero(lt(searchLen, 0x20)) { @@ -582,11 +582,11 @@ library LibBytes { i := add(i, 1) if iszero(lt(i, subjectSearchEnd)) { break } } - mstore(result, shr(5, sub(o, add(result, 0x20)))) // Store the length of `result`. - // Allocate memory for result. - // We allocate one more word, so this array can be recycled for {split}. - mstore(0x40, add(o, 0x20)) } + mstore(result, shr(5, sub(o, add(result, 0x20)))) // Store the length of `result`. + // Allocate memory for result. + // We allocate one more word, so this array can be recycled for {split}. + mstore(0x40, add(o, 0x20)) } } @@ -818,7 +818,8 @@ library LibBytes { let s := calldataload(add(a.offset, offset)) // Relative offset of `result` from `a.offset`. result.offset := add(a.offset, s) result.length := sub(a.length, s) - if or(shr(64, or(s, or(l, a.offset))), gt(offset, l)) { revert(l, 0x00) } + // forgefmt: disable-next-item + if or(shr(64, or(s, or(l, a.offset))), or(gt(offset, l), gt(s, l))) { revert(l, 0x00) } } } diff --git a/test/LibString.t.sol b/test/LibString.t.sol index fac778ea21..555393103d 100644 --- a/test/LibString.t.sol +++ b/test/LibString.t.sol @@ -1037,6 +1037,29 @@ contract LibStringTest is SoladyTest { assertTrue(_stringArraysAreSame(LibString.split("", ""), elements)); } + function testStringSplitDelimiterLongerThanSubject(uint256 dirty) public brutalizeMemory { + string[] memory elements = new string[](1); + elements[0] = "ab"; + for (uint256 t; t < 3; ++t) { + uint256 w = [uint256(0), 1, dirty][t]; + /// @solidity memory-safe-assembly + assembly { + mstore(0x00, w) + mstore(0x20, not(w)) + } + _misalignFreeMemoryPointer(); + string[] memory split = LibString.split("ab", "xyz"); + /// @solidity memory-safe-assembly + assembly { + mstore(0x00, not(0)) + mstore(0x20, not(0)) + } + assertTrue(_stringArraysAreSame(split, elements)); + _checkMemory(split[0]); + } + assertEq(LibString.indicesOf("ab", "xyz").length, 0); + } + function testStringConcat(string memory a, string memory b) public brutalizeMemory { string memory concatenated = LibString.concat(a, b); _checkMemory(concatenated); From 3422fbfa2e628b3ed57635bf099a3fa3c53d4c90 Mon Sep 17 00:00:00 2001 From: atarpara Date: Tue, 20 Oct 2026 13:58:18 +0530 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Optimize=20LibBytes.dy?= =?UTF-8?q?namicStructInCalldata=20bounds=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/LibBytes.sol | 12 ++++++++---- src/utils/g/LibBytes.sol | 12 ++++++++---- test/LibBytes.t.sol | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/utils/LibBytes.sol b/src/utils/LibBytes.sol index 839f0f129d..1e3f9069f1 100644 --- a/src/utils/LibBytes.sol +++ b/src/utils/LibBytes.sol @@ -811,11 +811,15 @@ library LibBytes { /// @solidity memory-safe-assembly assembly { let l := sub(a.length, 0x20) - let s := calldataload(add(a.offset, offset)) // Relative offset of `result` from `a.offset`. - result.offset := add(a.offset, s) - result.length := sub(a.length, s) + // Relative offset of `result` from `a.offset`. Reuses `result.length` to save a stack slot. + result.length := calldataload(add(a.offset, offset)) + result.offset := add(a.offset, result.length) + // Wraps if the relative offset exceeds `a.length`, caught below. + result.length := sub(a.length, result.length) + // `sub(result.offset, a.offset)` recovers the relative offset, even if the add wrapped. // forgefmt: disable-next-item - if or(shr(64, or(s, or(l, a.offset))), or(gt(offset, l), gt(s, l))) { revert(l, 0x00) } + if or(shr(64, or(result.length, or(sub(result.offset, a.offset), or(l, a.offset)))), + gt(offset, l)) { revert(l, 0x00) } } } diff --git a/src/utils/g/LibBytes.sol b/src/utils/g/LibBytes.sol index 589bd073fe..aec43c6934 100644 --- a/src/utils/g/LibBytes.sol +++ b/src/utils/g/LibBytes.sol @@ -815,11 +815,15 @@ library LibBytes { /// @solidity memory-safe-assembly assembly { let l := sub(a.length, 0x20) - let s := calldataload(add(a.offset, offset)) // Relative offset of `result` from `a.offset`. - result.offset := add(a.offset, s) - result.length := sub(a.length, s) + // Relative offset of `result` from `a.offset`. Reuses `result.length` to save a stack slot. + result.length := calldataload(add(a.offset, offset)) + result.offset := add(a.offset, result.length) + // Wraps if the relative offset exceeds `a.length`, caught below. + result.length := sub(a.length, result.length) + // `sub(result.offset, a.offset)` recovers the relative offset, even if the add wrapped. // forgefmt: disable-next-item - if or(shr(64, or(s, or(l, a.offset))), or(gt(offset, l), gt(s, l))) { revert(l, 0x00) } + if or(shr(64, or(result.length, or(sub(result.offset, a.offset), or(l, a.offset)))), + gt(offset, l)) { revert(l, 0x00) } } } diff --git a/test/LibBytes.t.sol b/test/LibBytes.t.sol index 244013ac0d..10c0808db2 100644 --- a/test/LibBytes.t.sol +++ b/test/LibBytes.t.sol @@ -293,6 +293,33 @@ contract LibBytesTest is SoladyTest { assertEq(LibBytes.bytesInCalldata(p, 0x40), expected.data); } + function testDynamicStructInCalldataBounds(uint256 s, uint256 n) public { + n = (n % 5) << 5; + if (_randomChance(2)) s = s % (n + 0x60); + bytes memory a = abi.encodePacked(s, new bytes(n)); + (bool ok, bytes memory r) = + address(this).call(abi.encodeWithSignature("_dynamicStructInCalldataBounds(bytes)", a)); + assertEq(ok, s <= a.length); + if (ok) { + (uint256 start, uint256 end) = abi.decode(r, (uint256, uint256)); + assertEq(start, s); + assertEq(end, a.length); + } + } + + function _dynamicStructInCalldataBounds(bytes calldata a) + public + pure + returns (uint256 start, uint256 end) + { + bytes calldata p = LibBytes.dynamicStructInCalldata(a, 0); + /// @solidity memory-safe-assembly + assembly { + start := sub(p.offset, a.offset) + end := add(start, p.length) + } + } + function testBytesInCalldata() public { this._testBytesInCalldata(abi.encode("hello"), 0x00, "hello"); } From 2436f00840d0c5b3bc9a868fccc78e7f20a29fca Mon Sep 17 00:00:00 2001 From: atarpara Date: Tue, 20 Oct 2026 13:59:52 +0530 Subject: [PATCH 4/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Move=20LibBytes=20edge?= =?UTF-8?q?=20case=20tests=20into=20LibBytes.t.sol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/LibBytes.t.sol | 41 +++++++++++++++++ test/LibBytesEdgeCases.t.sol | 88 ------------------------------------ 2 files changed, 41 insertions(+), 88 deletions(-) delete mode 100644 test/LibBytesEdgeCases.t.sol diff --git a/test/LibBytes.t.sol b/test/LibBytes.t.sol index 10c0808db2..b22ecc1022 100644 --- a/test/LibBytes.t.sol +++ b/test/LibBytes.t.sol @@ -3,6 +3,7 @@ pragma solidity ^0.8.4; import "./utils/SoladyTest.sol"; import {LibBytes} from "../src/utils/LibBytes.sol"; +import {EfficientHashLib} from "../src/utils/EfficientHashLib.sol"; contract BytesStore { LibBytes.BytesStorage internal value; @@ -307,6 +308,22 @@ contract LibBytesTest is SoladyTest { } } + function testDynamicStructInCalldataMalformedOffset() public { + // 64 bytes, with the struct offset word set to 65 (> `a.length`). + bytes memory a = abi.encode(uint256(65), uint256(0)); + (bool ok, bytes memory reason) = + address(this).call(abi.encodeWithSignature("_dynamicStructInCalldataBounds(bytes)", a)); + assertFalse(ok); + assertEq(reason, ""); + } + + function testDynamicStructInCalldataValid() public { + (uint256 start, uint256 end) = + this._dynamicStructInCalldataBounds(abi.encode(uint256(0x20), uint256(123))); + assertEq(start, 0x20); + assertEq(end, 0x40); + } + function _dynamicStructInCalldataBounds(bytes calldata a) public pure @@ -320,6 +337,30 @@ contract LibBytesTest is SoladyTest { } } + function testSplitNullPathCleanScratch() public { + bytes[] memory p = LibBytes.split("ab", "xyz"); + assertEq(p.length, 1); + assertEq(p[0], "ab"); + } + + function testSplitNullPathAfterHash() public { + // Dirties the scratch space before the split. + EfficientHashLib.hash(uint256(1)); + bytes[] memory p = LibBytes.split("ab", "xyz"); + assertEq(p.length, 1); + assertEq(p[0], "ab"); + } + + function testSplitNullPathTwoConsecutive() public { + // The first split leaves the scratch space dirty for the second. + bytes[] memory a = LibBytes.split("ab", "xyz"); + bytes[] memory b = LibBytes.split("cd", "wxyz"); + assertEq(a.length, 1); + assertEq(a[0], "ab"); + assertEq(b.length, 1); + assertEq(b[0], "cd"); + } + function testBytesInCalldata() public { this._testBytesInCalldata(abi.encode("hello"), 0x00, "hello"); } diff --git a/test/LibBytesEdgeCases.t.sol b/test/LibBytesEdgeCases.t.sol deleted file mode 100644 index 4284804535..0000000000 --- a/test/LibBytesEdgeCases.t.sol +++ /dev/null @@ -1,88 +0,0 @@ -// SPDX-License-Identifier: MIT - -pragma solidity ^0.8.4; - -import "./utils/forge-std/Test.sol"; -import {LibBytes} from "../src/utils/LibBytes.sol"; -import {EfficientHashLib} from "../src/utils/EfficientHashLib.sol"; - -/// @dev Tests for LibBytes edge cases: -/// - `split` when the delimiter is longer than the subject, -/// - `dynamicStructInCalldata` with a malformed struct offset. -contract LibBytesEdgeCasesTest is Test { - /// ------------------------------------------------------------------ - /// `split` with delimiter longer than subject. - /// Previously, `indicesOf` returned a null pointer on this path, and - /// `split` read the array length from the scratch space at 0x00, - /// which is not guaranteed to be zero. - /// ------------------------------------------------------------------ - /// Clean-scratch sanity: the null path works when scratch is zero. - function testSplitNullPathCleanScratch() public pure { - bytes[] memory p = LibBytes.split("ab", "xyz"); - require(p.length == 1, "length"); - require(bytes(p[0]).length == 2, "element length"); - } - - /// Scratch dirtied by a prior call to another library function. - /// Before the fix: corrupted result or OOG. After the fix: correct result. - function testSplitNullPathAfterHash() public { - bytes32 h = EfficientHashLib.hash(uint256(1)); - h; // silence unused - bytes[] memory p = LibBytes.split("ab", "xyz"); - assertEq(p.length, 1, "array length must be 1"); - assertEq(bytes(p[0]), "ab", "element must equal subject"); - } - - /// Two consecutive null-path splits: the first split itself leaves - /// scratch dirty for the second. Before the fix: OOG revert. - function testSplitNullPathTwoConsecutive() public pure { - bytes[] memory a = LibBytes.split("ab", "xyz"); - require(a.length == 1, "first split length"); - require(bytes(a[0]).length == 2, "first split element"); - bytes[] memory b = LibBytes.split("cd", "wxyz"); - require(b.length == 1, "second split length"); - require(bytes(b[0]).length == 2, "second split element"); - } - - /// ------------------------------------------------------------------ - /// `dynamicStructInCalldata` with a malformed struct offset. - /// Previously, a struct offset `s > a.length` was not rejected, and - /// `result.length = a.length - s` wrapped around to a huge value. - /// ------------------------------------------------------------------ - - Harness harness; - - function setUp() public { - harness = new Harness(); - } - - /// Malformed struct offset: a = 64 bytes, first word = 65 (> a.length). - /// Before the fix: no revert from LibBytes, returned slice had - /// length 2**256 - 1. After the fix: reverts with an empty reason - /// (the assembly `revert(l, 0x00)` bounds check). - function testDynamicStructInCalldataMalformedOffset() public { - bytes memory payload = abi.encode(uint256(65), uint256(0)); // 64 bytes, s = 65 - (bool ok, bytes memory reason) = - address(harness).call(abi.encodeWithSelector(Harness.f.selector, payload)); - assertFalse(ok, "must revert on out-of-bounds struct offset"); - assertEq(reason, bytes(""), "revert must come from LibBytes bounds check"); - } - - /// Valid input sanity: must NOT revert. - function testDynamicStructInCalldataValid() public view { - bytes memory payload = abi.encode(uint256(0x20), uint256(123)); - harness.f(payload); - } -} - -contract Harness { - /// Re-encodes the returned slice so the wrapped length is actually consumed. - function f(bytes calldata a) external pure returns (bytes calldata) { - bytes calldata s = LibBytes.dynamicStructInCalldata(a, 0); - // Touch the slice: reading its length forces use of the wrapped value. - require( - s.length < 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, "wrapped" - ); - return s; - } -}