From 30fb63e5ca5de0aa163a90a21f144f470fd41f6d Mon Sep 17 00:00:00 2001 From: nonergodic Date: Thu, 11 Jan 2024 02:16:58 -0800 Subject: [PATCH 1/3] fix my sloppy oversight of asUint8 missing unchecked suffix in asBoolUnchecked --- src/libraries/BytesParsing.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/BytesParsing.sol b/src/libraries/BytesParsing.sol index 7c644f6..21710a5 100644 --- a/src/libraries/BytesParsing.sol +++ b/src/libraries/BytesParsing.sol @@ -96,7 +96,7 @@ library BytesParsing { bytes memory encoded, uint offset ) internal pure returns (bool, uint) { - (uint8 val, uint nextOffset) = asUint8(encoded, offset); + (uint8 val, uint nextOffset) = asUint8Unchecked(encoded, offset); if (val & 0xfe != 0) revert InvalidBoolVal(val); From 2285c2741261386d281ad6953f83e9306fff1aba Mon Sep 17 00:00:00 2001 From: nonergodic Date: Thu, 11 Jan 2024 03:56:20 -0800 Subject: [PATCH 2/3] fix remove assembly which fails to account for dirty upper 31 bytes --- src/libraries/BytesParsing.sol | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/libraries/BytesParsing.sol b/src/libraries/BytesParsing.sol index 21710a5..16d30d6 100644 --- a/src/libraries/BytesParsing.sol +++ b/src/libraries/BytesParsing.sol @@ -96,15 +96,11 @@ library BytesParsing { bytes memory encoded, uint offset ) internal pure returns (bool, uint) { - (uint8 val, uint nextOffset) = asUint8Unchecked(encoded, offset); - if (val & 0xfe != 0) - revert InvalidBoolVal(val); + (uint8 ret, uint nextOffset) = asUint8Unchecked(encoded, offset); + if (ret & 0xfe != 0) + revert InvalidBoolVal(ret); - bool ret; - assembly ("memory-safe") { - ret := val - } - return (ret, nextOffset); + return (ret != 0, nextOffset); } function asBool( From ff5947d5ecd9f8c38b74adc22aff74082b216faa Mon Sep 17 00:00:00 2001 From: nonergodic Date: Thu, 11 Jan 2024 07:21:25 -0800 Subject: [PATCH 3/3] hopefully more efficient impl --- src/libraries/BytesParsing.sol | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/libraries/BytesParsing.sol b/src/libraries/BytesParsing.sol index 16d30d6..a356cdf 100644 --- a/src/libraries/BytesParsing.sol +++ b/src/libraries/BytesParsing.sol @@ -96,11 +96,17 @@ library BytesParsing { bytes memory encoded, uint offset ) internal pure returns (bool, uint) { - (uint8 ret, uint nextOffset) = asUint8Unchecked(encoded, offset); - if (ret & 0xfe != 0) - revert InvalidBoolVal(ret); + (uint8 val, uint nextOffset) = asUint8Unchecked(encoded, offset); + if (val & 0xfe != 0) + revert InvalidBoolVal(val); - return (ret != 0, nextOffset); + uint cleanedVal = uint(val); + bool ret; + //skip 2x iszero opcode + assembly ("memory-safe") { + ret := cleanedVal + } + return (ret, nextOffset); } function asBool(