Skip to content

Commit e3ffae7

Browse files
fix: prevent out-of-bounds access in bytesToBytes32 (#742)
Co-authored-by: DaniPopes <[email protected]>
1 parent a906201 commit e3ffae7

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/StdStorage.sol

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ library stdStorageSafe {
282282
function bytesToBytes32(bytes memory b, uint256 offset) private pure returns (bytes32) {
283283
bytes32 out;
284284

285-
uint256 max = b.length > 32 ? 32 : b.length;
285+
// Cap read length by remaining bytes from `offset`, and at most 32 bytes to avoid out-of-bounds
286+
uint256 max = b.length > offset ? b.length - offset : 0;
287+
if (max > 32) {
288+
max = 32;
289+
}
286290
for (uint256 i = 0; i < max; i++) {
287291
out |= bytes32(b[offset + i] & 0xFF) >> (i * 8);
288292
}

0 commit comments

Comments
 (0)