Skip to content

Commit 3f37ada

Browse files
authored
Fix byte lookup when decoding length of RLP structure (#6051)
1 parent 806165f commit 3f37ada

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

.changeset/yellow-clowns-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': patch
3+
---
4+
5+
`RLP`: Fix RLP encoding validity check when decoding long lists or strings

contracts/utils/RLP.sol

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,10 @@ library RLP {
351351
// Case: Long string (>55 bytes)
352352
uint256 lengthLength = prefix - SHORT_OFFSET - SHORT_THRESHOLD;
353353

354-
require(itemLength > lengthLength && bytes1(item.load(0)) != 0x00, RLPInvalidEncoding());
354+
bytes32 lenChunk = item.load(1);
355+
require(itemLength > lengthLength && bytes1(lenChunk) != 0x00, RLPInvalidEncoding());
355356

356-
uint256 len = uint256(item.load(1)) >> (256 - 8 * lengthLength);
357+
uint256 len = uint256(lenChunk) >> (256 - 8 * lengthLength);
357358
require(len > SHORT_THRESHOLD && itemLength > lengthLength + len, RLPInvalidEncoding());
358359

359360
return (lengthLength + 1, len, ItemType.Data);
@@ -369,10 +370,10 @@ library RLP {
369370
// Case: Long list
370371
uint256 lengthLength = prefix - LONG_OFFSET - SHORT_THRESHOLD;
371372

372-
require(itemLength > lengthLength, RLPInvalidEncoding());
373-
require(bytes1(item.load(0)) != 0x00);
373+
bytes32 lenChunk = item.load(1);
374+
require(itemLength > lengthLength && bytes1(lenChunk) != 0x00, RLPInvalidEncoding());
374375

375-
uint256 len = uint256(item.load(1)) >> (256 - 8 * lengthLength);
376+
uint256 len = uint256(lenChunk) >> (256 - 8 * lengthLength);
376377
require(len > SHORT_THRESHOLD && itemLength > lengthLength + len, RLPInvalidEncoding());
377378

378379
return (lengthLength + 1, len, ItemType.List);

0 commit comments

Comments
 (0)