diff --git a/contracts/contracts/CBOR.sol b/contracts/contracts/CBOR.sol index 3eac3356..51bab564 100644 --- a/contracts/contracts/CBOR.sol +++ b/contracts/contracts/CBOR.sol @@ -20,7 +20,7 @@ error CBOR_Error_InvalidUintSize(uint8); /// CBOR parsed value is out of expected range error CBOR_Error_ValueOutOfRange(); -error CBOR_Error_BytesTooLong(); +error CBOR_Error_BytesTooLong(uint256 byteLength); function encodeUint(uint256 value) pure returns (bytes memory) { // NOTE: we don't follow bignum tagged encoding @@ -38,7 +38,7 @@ function encodeUint(uint256 value) pure returns (bytes memory) { } else if (value <= type(uint128).max) { return abi.encodePacked(uint8(0x50), uint128(value)); } - return abi.encodePacked(uint8(0x58), uint(32), value); + return abi.encodePacked(uint8(0x58), uint256(32), value); } function encodeBytes(bytes memory in_bytes) @@ -62,7 +62,7 @@ function encodeBytes(bytes memory in_bytes) return abi.encodePacked(uint8(0x59), uint16(in_bytes.length), in_bytes); } // We assume Solidity won't be encoding anything larger than 64kb - revert CBOR_Error_BytesTooLong(); + revert CBOR_Error_BytesTooLong(in_bytes.length); } function parseMapStart(bytes memory in_data, uint256 in_offset) diff --git a/contracts/test/cbor.spec.ts b/contracts/test/cbor.spec.ts index 66089858..850cbc91 100644 --- a/contracts/test/cbor.spec.ts +++ b/contracts/test/cbor.spec.ts @@ -25,7 +25,9 @@ describe('CBOR', () => { }); it('Bytes encoding', async () => { - for (let i = 0; i < 2048; i = i + (1 + i / 10)) { + //for (let i = 0; i < 0xFFFF; i = i + (1 + i / 10)) { + for (let i = 0; i <= 16; i += 1) { + const nBytes = 1 << i; const input = randomBytes(i); const result = await contract.testBytesEncoding(input); const output = cborDecode(getBytes(result)); @@ -34,14 +36,26 @@ describe('CBOR', () => { } }); + it('Bytes encoding (errors)', async () => { + const length = 0xffff + 1; + const bytes = randomBytes(length); + await expect(contract.testBytesEncoding(bytes)).revertedWithCustomError( + contract, + 'CBOR_Error_BytesTooLong', + ); + }); + /// Verifies that bigints (byte encoded) can be parsed from 1 to 255 bits it('Parse uint (bytes)', async () => { - for( let i = 0n; i < 256n; i += 1n ) { - const value = 1n<