Skip to content

Commit

Permalink
Make solady.js cdDecompress consistent with LibZip.sol on invalid inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Aug 28, 2023
1 parent efd6317 commit c9e7f53
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
25 changes: 13 additions & 12 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -629,19 +629,20 @@ LibStringTest:testToStringZero() (gas: 1218)
LibStringTest:testToStringZeroBrutalized() (gas: 597698)
LibStringTest:testToStringZeroRightPadded(uint256) (runs: 256, μ: 728906, ~: 597374)
LibStringTest:test__codesize() (gas: 39660)
LibZipTest:testCdCompress() (gas: 166366)
LibZipTest:testCdCompressDecompress(bytes) (runs: 256, μ: 759866, ~: 636344)
LibZipTest:testCdCompressDecompress(uint256) (runs: 256, μ: 785156, ~: 697190)
LibZipTest:testCdCompress() (gas: 166411)
LibZipTest:testCdCompressDecompress(bytes) (runs: 256, μ: 777105, ~: 652974)
LibZipTest:testCdCompressDecompress(uint256) (runs: 256, μ: 818345, ~: 708209)
LibZipTest:testCdDecompressOnInvalidInput() (gas: 1171)
LibZipTest:testCdFallback() (gas: 5691909)
LibZipTest:testCdFallback(bytes,uint256) (runs: 256, μ: 1222349, ~: 1032471)
LibZipTest:testCdFallbackDecompressor(bytes) (runs: 256, μ: 121244, ~: 117103)
LibZipTest:testCdFallbackDecompressor(uint256) (runs: 256, μ: 171359, ~: 158926)
LibZipTest:testCdFallbackMaskTrick(uint256,uint256) (runs: 256, μ: 690, ~: 663)
LibZipTest:testDecompressWontRevert(bytes) (runs: 256, μ: 722693, ~: 616344)
LibZipTest:testFlzCompressDecompress() (gas: 2148082)
LibZipTest:testFlzCompressDecompress(bytes) (runs: 256, μ: 843028, ~: 676046)
LibZipTest:testFlzCompressDecompress2() (gas: 1000434)
LibZipTest:test__codesize() (gas: 21473)
LibZipTest:testCdFallback(bytes,uint256) (runs: 256, μ: 1286982, ~: 1043718)
LibZipTest:testCdFallbackDecompressor(bytes) (runs: 256, μ: 121255, ~: 117359)
LibZipTest:testCdFallbackDecompressor(uint256) (runs: 256, μ: 166038, ~: 154779)
LibZipTest:testCdFallbackMaskTrick(uint256,uint256) (runs: 256, μ: 687, ~: 663)
LibZipTest:testDecompressWontRevert(bytes) (runs: 256, μ: 751503, ~: 630241)
LibZipTest:testFlzCompressDecompress() (gas: 2161618)
LibZipTest:testFlzCompressDecompress(bytes) (runs: 256, μ: 893887, ~: 686301)
LibZipTest:testFlzCompressDecompress2() (gas: 1013904)
LibZipTest:test__codesize() (gas: 21736)
MerkleProofLibTest:testEmptyCalldataHelpers() (gas: 1086)
MerkleProofLibTest:testVerifyMultiProof(bool,bool,bool,bool,bytes32) (runs: 256, μ: 779659, ~: 630526)
MerkleProofLibTest:testVerifyMultiProofForHeightOneTree(bool,bool,bool,bool,bool,bool[]) (runs: 256, μ: 33431, ~: 32418)
Expand Down
4 changes: 2 additions & 2 deletions js/solady.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
*/
LibZip.cdDecompress = function(data) {
data = hexString(data);
var o = "0x", i = 0, c, s;
var o = "0x", i = 0, j, c, s;

while (i < data.length) {
c = ((i < 4 * 2) * 0xff) ^ parseByte(data, i);
Expand All @@ -197,7 +197,7 @@
c = ((i < 4 * 2) * 0xff) ^ parseByte(data, i);
s = (c & 0x7f) + 1;
i += 2;
while (s--) o += byteToString((c >> 7) * 0xff);
for (j = 0; j < s; ++j) o += byteToString((c >> 7 && j < 32) * 0xff);
continue;
}
o += byteToString(c);
Expand Down
6 changes: 6 additions & 0 deletions js/solady.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ test("LibZip: Calldata compress", function() {
assertEq(solady.LibZip.cdCompress(data), expected);
});

test("LibZip: Calldata decompress on invalid input", function() {
var data = "0xffffffff00ff";
var expected = "0x0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
assertEq(solady.LibZip.cdDecompress(data), expected);
});

test("ERC1967Factory: ABI and address", function() {
function hashFnv32a(s) {
var h = 0x811c9dc5;
Expand Down
7 changes: 7 additions & 0 deletions test/LibZip.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ contract LibZipTest is SoladyTest {
assertEq(LibZip.cdCompress(data), expected);
}

function testCdDecompressOnInvalidInput() public {
bytes memory data = hex"ffffffff00ff";
bytes memory expected =
hex"0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
bytes memory decompressed = LibZip.cdDecompress(data);
}

function testDecompressWontRevert(bytes memory data) public brutalizeMemory {
data = LibZip.cdDecompress(data);
bytes memory compressed = LibZip.cdCompress(data);
Expand Down

0 comments on commit c9e7f53

Please sign in to comment.