From d2cbf95890e0b3d019068537f15e10eec86def59 Mon Sep 17 00:00:00 2001 From: gomesalexandre <17035424+gomesalexandre@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:28:53 +0100 Subject: [PATCH 1/6] fix: revert LibZip.flzDecompress when a FastLZ instruction is truncated Incomplete literal and match headers were decoded by reading past the payload, so callers received a short zero-padded bytes string. Those inputs now revert with TruncatedFastLZ. Co-authored-by: Cursor --- docs/utils/libzip.md | 13 ++++++++++++- src/utils/LibZip.sol | 18 +++++++++++++++++- test/LibZip.t.sol | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/docs/utils/libzip.md b/docs/utils/libzip.md index caa9bc6851..93308ba18f 100644 --- a/docs/utils/libzip.md +++ b/docs/utils/libzip.md @@ -12,6 +12,16 @@ FastLZ and calldata operations for convenience. +## Custom Errors + +### TruncatedFastLZ() + +```solidity +error TruncatedFastLZ() +``` + +The FastLZ payload ends before an instruction is complete. + ## Fast LZ Operations LZ77 implementation based on FastLZ. @@ -39,7 +49,8 @@ function flzDecompress(bytes memory data) returns (bytes memory result) ``` -Returns the decompressed `data`. +Returns the decompressed `data`. +Reverts with `TruncatedFastLZ` if an instruction runs past the end of `data`. ## Calldata Operations diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index e7ce354d89..2f694b31db 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -10,6 +10,13 @@ pragma solidity ^0.8.4; /// The accompanying solady.js library includes implementations of /// FastLZ and calldata operations for convenience. library LibZip { + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ + /* CUSTOM ERRORS */ + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ + + /// @dev The FastLZ payload ends before an instruction is complete. + error TruncatedFastLZ(); + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* FAST LZ OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ @@ -108,9 +115,14 @@ library LibZip { } /// @dev Returns the decompressed `data`. + /// Reverts with `TruncatedFastLZ` if an instruction runs past the end of `data`. function flzDecompress(bytes memory data) internal pure returns (bytes memory result) { /// @solidity memory-safe-assembly assembly { + function revertTruncated() { + mstore(0x00, 0x8bb83a49) // `TruncatedFastLZ()`. + revert(0x1c, 0x04) + } result := mload(0x40) let op := add(result, 0x20) let end := add(add(data, 0x20), mload(data)) @@ -119,13 +131,17 @@ library LibZip { let c := byte(0, w) let t := shr(5, c) if iszero(t) { + // Literal run: control byte plus `c + 1` payload bytes. + if lt(sub(end, data), add(2, c)) { revertTruncated() } mstore(op, mload(add(data, 1))) data := add(data, add(2, c)) op := add(op, add(1, c)) continue } + let g := eq(t, 7) + // Short match is 2 bytes. Long match (`t == 7`) is 3 bytes. + if lt(sub(end, data), add(2, g)) { revertTruncated() } for { - let g := eq(t, 7) let l := add(2, xor(t, mul(g, xor(t, add(7, byte(1, w)))))) // M let s := add(add(shl(8, and(0x1f, c)), byte(add(1, g), w)), 1) // R let r := sub(op, s) diff --git a/test/LibZip.t.sol b/test/LibZip.t.sol index 480393c43b..1da37c3a0d 100644 --- a/test/LibZip.t.sol +++ b/test/LibZip.t.sol @@ -220,6 +220,39 @@ contract LibZipTest is SoladyTest { assertEq(LibZip.flzDecompress(compressed), decompressed); } + /// @dev External wrapper so `vm.expectRevert` observes `LibZip.flzDecompress`. + function flzDecompress(bytes memory data) external pure returns (bytes memory) { + return LibZip.flzDecompress(data); + } + + function testFlzDecompressTruncatedReverts() public { + assertEq(this.flzDecompress(""), ""); + assertEq(this.flzDecompress(hex"02414243"), hex"414243"); + assertEq(this.flzDecompress(hex"024142430044"), hex"41424344"); + assertEq(this.flzDecompress(hex"026162632002"), hex"616263616263"); + assertEq(this.flzDecompress(hex"02616263e00002"), hex"616263616263616263616263"); + + // Instruction ends past the buffer. Unfixed code returns a short zero-padded + // bytes string (`0x00`, `0x410000`, `0x414200`) instead of reverting. + bytes4 truncated = LibZip.TruncatedFastLZ.selector; + vm.expectRevert(truncated); + this.flzDecompress(hex"00"); + vm.expectRevert(truncated); + this.flzDecompress(hex"02"); + vm.expectRevert(truncated); + this.flzDecompress(hex"0241"); + vm.expectRevert(truncated); + this.flzDecompress(hex"024142"); + vm.expectRevert(truncated); + this.flzDecompress(hex"1f"); + vm.expectRevert(truncated); + this.flzDecompress(hex"20"); + vm.expectRevert(truncated); + this.flzDecompress(hex"e000"); + vm.expectRevert(truncated); + this.flzDecompress(hex"02616263e000"); + } + function _expandedData(bytes memory data) internal returns (bytes memory) { unchecked { DynamicBufferLib.DynamicBuffer memory buffer; From 799e37024c71e5402df42702a5b81c102fd62661 Mon Sep 17 00:00:00 2001 From: gomesalexandre <17035424+gomesalexandre@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:42:23 +0100 Subject: [PATCH 2/6] fix: revert clz LibZip.flzDecompress on truncated FastLZ The Osaka copy walks FastLZ the same way as src/utils/LibZip.sol, so a short instruction still returned zero padding there. Co-authored-by: Cursor --- src/utils/clz/LibZip.sol | 18 +++++++++++++++++- test/clz/LibZip.t.sol | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/utils/clz/LibZip.sol b/src/utils/clz/LibZip.sol index 2c57e0797c..701090ec12 100644 --- a/src/utils/clz/LibZip.sol +++ b/src/utils/clz/LibZip.sol @@ -10,6 +10,13 @@ pragma solidity ^0.8.30; /// The accompanying solady.js library includes implementations of /// FastLZ and calldata operations for convenience. library LibZip { + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ + /* CUSTOM ERRORS */ + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ + + /// @dev The FastLZ payload ends before an instruction is complete. + error TruncatedFastLZ(); + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ /* FAST LZ OPERATIONS */ /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ @@ -108,9 +115,14 @@ library LibZip { } /// @dev Returns the decompressed `data`. + /// Reverts with `TruncatedFastLZ` if an instruction runs past the end of `data`. function flzDecompress(bytes memory data) internal pure returns (bytes memory result) { /// @solidity memory-safe-assembly assembly { + function revertTruncated() { + mstore(0x00, 0x8bb83a49) // `TruncatedFastLZ()`. + revert(0x1c, 0x04) + } result := mload(0x40) let op := add(result, 0x20) let end := add(add(data, 0x20), mload(data)) @@ -119,13 +131,17 @@ library LibZip { let c := byte(0, w) let t := shr(5, c) if iszero(t) { + // Literal run: control byte plus `c + 1` payload bytes. + if lt(sub(end, data), add(2, c)) { revertTruncated() } mstore(op, mload(add(data, 1))) data := add(data, add(2, c)) op := add(op, add(1, c)) continue } + let g := eq(t, 7) + // Short match is 2 bytes. Long match (`t == 7`) is 3 bytes. + if lt(sub(end, data), add(2, g)) { revertTruncated() } for { - let g := eq(t, 7) let l := add(2, xor(t, mul(g, xor(t, add(7, byte(1, w)))))) // M let s := add(add(shl(8, and(0x1f, c)), byte(add(1, g), w)), 1) // R let r := sub(op, s) diff --git a/test/clz/LibZip.t.sol b/test/clz/LibZip.t.sol index f6537ecbe4..5e267aa5f2 100644 --- a/test/clz/LibZip.t.sol +++ b/test/clz/LibZip.t.sol @@ -220,6 +220,39 @@ contract LibZiWithCLZTest is SoladyTest { assertEq(LibZip.flzDecompress(compressed), decompressed); } + /// @dev External wrapper so `vm.expectRevert` observes `LibZip.flzDecompress`. + function flzDecompress(bytes memory data) external pure returns (bytes memory) { + return LibZip.flzDecompress(data); + } + + function testFlzDecompressTruncatedReverts() public { + assertEq(this.flzDecompress(""), ""); + assertEq(this.flzDecompress(hex"02414243"), hex"414243"); + assertEq(this.flzDecompress(hex"024142430044"), hex"41424344"); + assertEq(this.flzDecompress(hex"026162632002"), hex"616263616263"); + assertEq(this.flzDecompress(hex"02616263e00002"), hex"616263616263616263616263"); + + // Instruction ends past the buffer. Unfixed code returns a short zero-padded + // bytes string (`0x00`, `0x410000`, `0x414200`) instead of reverting. + bytes4 truncated = LibZip.TruncatedFastLZ.selector; + vm.expectRevert(truncated); + this.flzDecompress(hex"00"); + vm.expectRevert(truncated); + this.flzDecompress(hex"02"); + vm.expectRevert(truncated); + this.flzDecompress(hex"0241"); + vm.expectRevert(truncated); + this.flzDecompress(hex"024142"); + vm.expectRevert(truncated); + this.flzDecompress(hex"1f"); + vm.expectRevert(truncated); + this.flzDecompress(hex"20"); + vm.expectRevert(truncated); + this.flzDecompress(hex"e000"); + vm.expectRevert(truncated); + this.flzDecompress(hex"02616263e000"); + } + function _expandedData(bytes memory data) internal returns (bytes memory) { unchecked { DynamicBufferLib.DynamicBuffer memory buffer; From 983d362637158411448f6d789cb78b04bca39ed2 Mon Sep 17 00:00:00 2001 From: gomesalexandre <17035424+gomesalexandre@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:29:32 +0100 Subject: [PATCH 3/6] fix: stop LibZip.cdDecompress from expanding a truncated RLE marker A payload that ends on the 0x00 marker loaded the missing control byte from past the end and appended a run of zeros or 0xff. That marker is now left unexpanded. Co-authored-by: Cursor --- docs/utils/libzip.md | 3 +- src/utils/LibZip.sol | 3 ++ test/LibZip.t.sol | 72 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/docs/utils/libzip.md b/docs/utils/libzip.md index 93308ba18f..7f56b4f126 100644 --- a/docs/utils/libzip.md +++ b/docs/utils/libzip.md @@ -85,7 +85,8 @@ function cdDecompress(bytes memory data) returns (bytes memory result) ``` -Returns the decompressed `data`. +Returns the decompressed `data`. +A trailing `0x00` marker with no control byte is not expanded. ### cdFallback() diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index 2f694b31db..dc5e25cf93 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -256,6 +256,7 @@ library LibZip { } /// @dev Returns the decompressed `data`. + /// A trailing `0x00` marker with no control byte is not expanded. function cdDecompress(bytes memory data) internal pure returns (bytes memory result) { /// @solidity memory-safe-assembly assembly { @@ -270,6 +271,8 @@ library LibZip { for { let i := add(0x20, data) } 1 {} { let c := mload(i) if iszero(byte(0, c)) { + // Marker with no control byte. Do not read past `end`. + if iszero(lt(add(i, 1), end)) { break } c := add(1, byte(1, c)) if iszero(gt(c, 0x80)) { i := add(i, 2) diff --git a/test/LibZip.t.sol b/test/LibZip.t.sol index 1da37c3a0d..bb5deb4344 100644 --- a/test/LibZip.t.sol +++ b/test/LibZip.t.sol @@ -183,6 +183,8 @@ contract LibZipTest is SoladyTest { data := add(data, 1) let c := byte(31, mload(data)) if iszero(c) { + // The marker is the last byte, so the control byte is not in `data`. + if iszero(lt(data, end)) { break } data := add(data, 1) let d := byte(31, mload(data)) // Fill with either 0xff or 0x00. @@ -428,6 +430,44 @@ contract LibZipTest is SoladyTest { assertEq(LibZip.cdCompress(data), expected); } + /// @dev Shrinks `data` to `len` bytes without clearing the following byte. + function _cdDecompressLen(bytes memory data, uint256 len) internal pure returns (bytes memory) { + /// @solidity memory-safe-assembly + assembly { + mstore(data, len) + } + return LibZip.cdDecompress(data); + } + + function testCdDecompressTruncatedRleStops() public { + // Lone marker. Stored `0xff` un-negates to `0x00`, and the control byte is absent. + // Unfixed code reads the next byte (zero padding, negated to `0xff`) and returns 128 bytes. + assertEq(LibZip.cdDecompress(hex"ff"), ""); + + // Literals plus a trailing marker. Unfixed code reads one `0x00` past the end + // and appends a one-byte zero run, returning `0x0102030400`. + assertEq(LibZip.cdDecompress(hex"fefdfcfb00"), hex"01020304"); + + // A non-zero control byte left in memory past `len` must not be consumed. + // `0x0a` would append 11 zeros. `0xff` would append a 128-byte run. + assertEq(_cdDecompressLen(hex"fefdfcfb000a", 5), hex"01020304"); + assertEq(_cdDecompressLen(hex"fefdfcfb00ff", 5), hex"01020304"); + + // Complete marker plus control, then a second marker with no control byte. + // The first run is two zeros. The trailing marker is not expanded. + assertEq(LibZip.cdDecompress(hex"fffeff"), hex"0000"); + + // Complete runs still decode, including a run that ends on the last byte. + assertEq(LibZip.cdDecompress(hex"ffff"), hex"00"); + assertEq(LibZip.cdDecompress(hex"ff7f"), hex"ff"); + assertEq( + LibZip.cdDecompress( + LibZip.cdCompress(hex"010203040000ffffffffffffffffffffffffffffffff") + ), + hex"010203040000ffffffffffffffffffffffffffffffff" + ); + } + function testCdDecompressOnInvalidInput() public { bytes memory data = hex"ffffffff00ff"; bytes memory expected = @@ -436,6 +476,38 @@ contract LibZipTest is SoladyTest { assertEq(decompressed, expected); } + function testCdDecompressTruncatedRle() public { + // Logical `11 22 33 44 00`. The final `0x00` is an RLE marker and the + // control byte is absent. Stored form negates the first 4 bytes. + // Unfixed code reads the padding as that control byte and appends one `0x00`. + assertEq(LibZip.cdDecompress(hex"eeddccbb00"), hex"11223344"); + + // Same marker, with `0x7f` (a 128-zero run) planted past the payload. + bytes memory planted = hex"eeddccbb00"; + /// @solidity memory-safe-assembly + assembly { + mstore8(add(add(planted, 0x20), mload(planted)), 0x7f) + } + assertEq(LibZip.cdDecompress(planted), hex"11223344"); + + // Lone logical `0x00`, stored as `0xff`. Negating the first 4 bytes turns + // the zero padding into `0xff`, and unfixed code appends a 128-byte run. + assertEq(LibZip.cdDecompress(hex"ff"), hex""); + + // `00 00 11 00`: one zero, the literal `0x11`, then a truncated marker. + assertEq(LibZip.cdDecompress(hex"ffffeeff"), hex"0011"); + + // Complete headers still expand. `00 00` is one zero, `00 9f` is 32 `0xff`, + // `00 7f` is 128 zeros. First 4 stored bytes are negated. + assertEq(LibZip.cdDecompress(hex"eeddccbb0000"), hex"1122334400"); + assertEq( + LibZip.cdDecompress(hex"ff60"), + hex"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + ); + assertEq(LibZip.cdDecompress(hex"ff80"), new bytes(128)); + assertEq(LibZip.cdDecompress(LibZip.cdCompress(hex"1122334400ff")), hex"1122334400ff"); + } + function testDecompressWontRevert(bytes memory data) public brutalizeMemory { data = LibZip.cdDecompress(data); bytes memory compressed = LibZip.cdCompress(data); From 9f5831b5c76a1fe69380a53bb02b477ccd362be7 Mon Sep 17 00:00:00 2001 From: gomesalexandre <17035424+gomesalexandre@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:50:04 +0100 Subject: [PATCH 4/6] fix: stop clz LibZip.cdDecompress on a truncated RLE marker The Osaka copy read the missing control byte from past the payload, same as src/utils/LibZip.sol. Co-authored-by: Cursor --- src/utils/clz/LibZip.sol | 3 +++ test/clz/LibZip.t.sol | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/utils/clz/LibZip.sol b/src/utils/clz/LibZip.sol index 701090ec12..ed6874eabe 100644 --- a/src/utils/clz/LibZip.sol +++ b/src/utils/clz/LibZip.sol @@ -254,6 +254,7 @@ library LibZip { } /// @dev Returns the decompressed `data`. + /// A trailing `0x00` marker with no control byte is not expanded. function cdDecompress(bytes memory data) internal pure returns (bytes memory result) { /// @solidity memory-safe-assembly assembly { @@ -268,6 +269,8 @@ library LibZip { for { let i := add(0x20, data) } 1 {} { let c := mload(i) if iszero(byte(0, c)) { + // Marker with no control byte. Do not read past `end`. + if iszero(lt(add(i, 1), end)) { break } c := add(1, byte(1, c)) if iszero(gt(c, 0x80)) { i := add(i, 2) diff --git a/test/clz/LibZip.t.sol b/test/clz/LibZip.t.sol index 5e267aa5f2..dba0b2f850 100644 --- a/test/clz/LibZip.t.sol +++ b/test/clz/LibZip.t.sol @@ -183,6 +183,8 @@ contract LibZiWithCLZTest is SoladyTest { data := add(data, 1) let c := byte(31, mload(data)) if iszero(c) { + // The marker is the last byte, so the control byte is not in `data`. + if iszero(lt(data, end)) { break } data := add(data, 1) let d := byte(31, mload(data)) // Fill with either 0xff or 0x00. @@ -253,6 +255,22 @@ contract LibZiWithCLZTest is SoladyTest { this.flzDecompress(hex"02616263e000"); } + function testCdDecompressTruncatedRle() public { + // Logical `11 22 33 44 00`. The final `0x00` is an RLE marker and the + // control byte is absent. Stored form negates the first 4 bytes. + // Unfixed code reads the padding as that control byte and appends one `0x00`. + assertEq(LibZip.cdDecompress(hex"eeddccbb00"), hex"11223344"); + assertEq(LibZip.cdDecompress(hex"ff"), ""); + assertEq(LibZip.cdDecompress(hex"ffffeeff"), hex"0011"); + assertEq(LibZip.cdDecompress(hex"eeddccbb0000"), hex"1122334400"); + assertEq( + LibZip.cdDecompress(hex"ff60"), + hex"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + ); + assertEq(LibZip.cdDecompress(hex"ff80"), new bytes(128)); + assertEq(LibZip.cdDecompress(LibZip.cdCompress(hex"1122334400ff")), hex"1122334400ff"); + } + function _expandedData(bytes memory data) internal returns (bytes memory) { unchecked { DynamicBufferLib.DynamicBuffer memory buffer; From a773fbd609ace83f774fee3e9ea2c004dd97eeb7 Mon Sep 17 00:00:00 2001 From: gomesalexandre <17035424+gomesalexandre@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:13:08 +0100 Subject: [PATCH 5/6] fix: stop LibZip.cdFallback from expanding a truncated RLE marker Calldata that ends on the 0x00 marker loaded the missing control byte from past the end and appended a run. That marker is now left unexpanded. Co-authored-by: Cursor --- docs/utils/libzip.md | 3 ++- src/utils/LibZip.sol | 3 +++ test/LibZip.t.sol | 59 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/docs/utils/libzip.md b/docs/utils/libzip.md index 7f56b4f126..a35851a9ac 100644 --- a/docs/utils/libzip.md +++ b/docs/utils/libzip.md @@ -101,4 +101,5 @@ receive() external payable {} Silence compiler warning to add a `receive` function. ``` For efficiency, this function will directly return the results, terminating the context. -If called internally, it must be called at the end of the function. \ No newline at end of file +If called internally, it must be called at the end of the function. +A trailing `0x00` marker with no control byte is not expanded. \ No newline at end of file diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index dc5e25cf93..ed796cdb99 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -316,6 +316,7 @@ library LibZip { /// ``` /// For efficiency, this function will directly return the results, terminating the context. /// If called internally, it must be called at the end of the function. + /// A trailing `0x00` marker with no control byte is not expanded. function cdFallback() internal { /// @solidity memory-safe-assembly assembly { @@ -326,6 +327,8 @@ library LibZip { let c := byte(0, xor(add(i, f), calldataload(i))) i := add(i, 1) if iszero(c) { + // Marker with no control byte. Do not read past calldata. + if iszero(lt(i, calldatasize())) { break } let d := byte(0, xor(add(i, f), calldataload(i))) i := add(i, 1) // Fill with either 0xff or 0x00. diff --git a/test/LibZip.t.sol b/test/LibZip.t.sol index bb5deb4344..c8a0e7301f 100644 --- a/test/LibZip.t.sol +++ b/test/LibZip.t.sol @@ -515,6 +515,51 @@ contract LibZipTest is SoladyTest { assertEq(decompressed, data); } + function testCdFallbackTruncatedRleStops() public { + // Logical `01 02 03 04 00`. The final `0x00` is an RLE marker and the + // control byte is absent. Stored form negates the first 4 bytes. + // Unfixed code reads the zero padding as that control byte and appends one `0x00`. + assertEq(_cdFallbackEcho(hex"fefdfcfb00"), hex"01020304"); + + // Same shape: logical `11 22 33 44 00`. + assertEq(_cdFallbackEcho(hex"eeddccbb00"), hex"11223344"); + + // One zero, the literal `0x11`, then a marker on the last byte. + // The missing control sits at index 4, so padding is not negated. + assertEq(_cdFallbackEcho(hex"ffffeeff"), hex"0011"); + + // A control byte that is present, including as the last byte, still expands. + // `00 00` is one zero. `00 80` is one `0xff`. + assertEq(_cdFallbackEcho(hex"fefdfcfb0000"), hex"0102030400"); + assertEq(_cdFallbackEcho(hex"fefdfcfb0080"), hex"01020304ff"); + + // Lone marker. Stored `0xff` un-negates to `0x00`, and the control byte is absent. + // Unfixed code reads the zero padding, negates it to `0xff`, and returns 128 bytes. + assertEq(_cdFallbackEcho(hex"ff"), ""); + + // Two zeros, then a marker whose missing control is still inside the negated prefix. + // Unfixed code appends 128 `0xff` bytes from that padding. + assertEq(_cdFallbackEcho(hex"fffeff"), hex"0000"); + + // Complete headers still expand. `00 00` is one zero, `00 9f` is 32 `0xff`, + // `00 7f` is 128 zeros. First 4 stored bytes are negated. + assertEq(_cdFallbackEcho(hex"ffff"), hex"00"); + assertEq(_cdFallbackEcho(hex"ff7f"), hex"ff"); + assertEq(_cdFallbackEcho(hex"ff80"), new bytes(128)); + assertEq( + _cdFallbackEcho(hex"ff60"), + hex"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + ); + assertEq(_cdFallbackEcho(LibZip.cdCompress(hex"1122334400ff")), hex"1122334400ff"); + } + + function _cdFallbackEcho(bytes memory compressed) internal returns (bytes memory result) { + MockCdFallbackEcho echo = new MockCdFallbackEcho(); + bool success; + (success, result) = address(echo).call(compressed); + assertTrue(success); + } + function testCdFallback() public { MockCd mockCd = new MockCd(); _testCdFallback(mockCd); @@ -646,3 +691,17 @@ contract LibZipTest is SoladyTest { assertEq(computed, expected); } } + +/// @dev Returns the bytes `LibZip.cdFallback` passes to its delegatecall. +contract MockCdFallbackEcho { + fallback() external payable { + assembly { + if sload(0) { + calldatacopy(0x00, 0x00, calldatasize()) + return(0x00, calldatasize()) + } + sstore(0, 1) + } + LibZip.cdFallback(); + } +} From 9951036439fa71fe07baf896ec5ff02c7a9f749e Mon Sep 17 00:00:00 2001 From: gomesalexandre <17035424+gomesalexandre@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:25:37 +0100 Subject: [PATCH 6/6] fix: stop clz LibZip.cdFallback on a truncated RLE marker The Osaka copy loaded the missing control byte from past calldata, same as src/utils/LibZip.sol. Co-authored-by: Cursor --- src/utils/clz/LibZip.sol | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/utils/clz/LibZip.sol b/src/utils/clz/LibZip.sol index ed6874eabe..69b2adaf41 100644 --- a/src/utils/clz/LibZip.sol +++ b/src/utils/clz/LibZip.sol @@ -314,6 +314,7 @@ library LibZip { /// ``` /// For efficiency, this function will directly return the results, terminating the context. /// If called internally, it must be called at the end of the function. + /// A trailing `0x00` marker with no control byte is not expanded. function cdFallback() internal { /// @solidity memory-safe-assembly assembly { @@ -324,6 +325,8 @@ library LibZip { let c := byte(0, xor(add(i, f), calldataload(i))) i := add(i, 1) if iszero(c) { + // Marker with no control byte. Do not read past calldata. + if iszero(lt(i, calldatasize())) { break } let d := byte(0, xor(add(i, f), calldataload(i))) i := add(i, 1) // Fill with either 0xff or 0x00.