@@ -221,8 +221,10 @@ abstract contract MultiSignerERC7913 is AbstractSigner {
221221 bytes calldata signature
222222 ) internal view virtual override returns (bool ) {
223223 if (signature.length == 0 ) return false ; // For ERC-7739 compatibility
224- (bytes [] memory signers , bytes [] memory signatures ) = abi.decode (signature, (bytes [], bytes []));
225- return _validateThreshold (signers) && _validateSignatures (hash, signers, signatures);
224+ (bool success , bytes [] calldata signers , bytes [] calldata signatures ) = _tryDecodeMultisignatureCalldata (
225+ signature
226+ );
227+ return success && _validateThreshold (signers) && _validateSignatures (hash, signers, signatures);
226228 }
227229
228230 /**
@@ -238,8 +240,8 @@ abstract contract MultiSignerERC7913 is AbstractSigner {
238240 */
239241 function _validateSignatures (
240242 bytes32 hash ,
241- bytes [] memory signers ,
242- bytes [] memory signatures
243+ bytes [] calldata signers ,
244+ bytes [] calldata signatures
243245 ) internal view virtual returns (bool valid ) {
244246 for (uint256 i = 0 ; i < signers.length ; ++ i) {
245247 if (! isSigner (signers[i])) {
@@ -253,7 +255,77 @@ abstract contract MultiSignerERC7913 is AbstractSigner {
253255 * @dev Validates that the number of signers meets the {threshold} requirement.
254256 * Assumes the signers were already validated. See {_validateSignatures} for more details.
255257 */
256- function _validateThreshold (bytes [] memory validatingSigners ) internal view virtual returns (bool ) {
258+ function _validateThreshold (bytes [] calldata validatingSigners ) internal view virtual returns (bool ) {
257259 return validatingSigners.length >= threshold ();
258260 }
261+
262+ /**
263+ * @dev Decodes an `abi.encode(bytes[], bytes[])` multisignature payload from calldata without memory
264+ * allocation. Returns `success = false` on malformed encoding so callers can report an invalid
265+ * signature instead of reverting during validation (see ERC-4337 `SIG_VALIDATION_FAILED` semantics).
266+ */
267+ function _tryDecodeMultisignatureCalldata (
268+ bytes calldata signature
269+ ) private pure returns (bool success , bytes [] calldata signers , bytes [] calldata signatures ) {
270+ unchecked {
271+ uint256 bufferLength = signature.length ;
272+
273+ // Check #1: Theoretical minimum length of a valid multisignature encoding is 0x40 bytes.
274+ // 64 bytes of zero is a valid encoding for two empty arrays.
275+ if (bufferLength < 0x40 ) return (false , _emptyBytesArray (), _emptyBytesArray ());
276+
277+ // Read the offset pointers to the signers and signatures arrays
278+ // Read is done in assembly to avoid the cost of creating calldata slices.
279+ uint256 signersOffset;
280+ uint256 signaturesOffset;
281+ assembly ("memory-safe" ) {
282+ signersOffset := calldataload (signature.offset)
283+ signaturesOffset := calldataload (add (signature.offset, 0x20 ))
284+ }
285+
286+ // Check #2: The length fields that the offset pointers point to must be within the bounds of the buffer.
287+ if (signersOffset > bufferLength - 0x20 || signaturesOffset > bufferLength - 0x20 )
288+ return (false , _emptyBytesArray (), _emptyBytesArray ());
289+
290+ // Read the length fields
291+ // Read is done in assembly to avoid the cost of creating calldata slices.
292+ uint256 signersLength;
293+ uint256 signaturesLength;
294+ assembly ("memory-safe" ) {
295+ signersLength := calldataload (add (signature.offset, signersOffset))
296+ signaturesLength := calldataload (add (signature.offset, signaturesOffset))
297+ }
298+
299+ // Data is just after the length fields
300+ uint256 signersDataOffset = signersOffset + 0x20 ;
301+ uint256 signaturesDataOffset = signaturesOffset + 0x20 ;
302+
303+ // Check #3 & #4:
304+ // - Cap lengths at 2**64-1 (Solidity's own dynamic-array limit) so `length * 0x20` cannot overflow.
305+ // - The data for each array must fit within the bounds of the buffer.
306+ if (
307+ signersLength > type (uint64 ).max ||
308+ signaturesLength > type (uint64 ).max ||
309+ 0x20 * signersLength > bufferLength - signersDataOffset ||
310+ 0x20 * signaturesLength > bufferLength - signaturesDataOffset
311+ ) return (false , _emptyBytesArray (), _emptyBytesArray ());
312+
313+ // Assembly cast
314+ assembly ("memory-safe" ) {
315+ success := 1 // true
316+ signers.offset := add (signature.offset, signersDataOffset)
317+ signers.length := signersLength
318+ signatures.offset := add (signature.offset, signaturesDataOffset)
319+ signatures.length := signaturesLength
320+ }
321+ }
322+ }
323+
324+ /// @dev Returns an empty `bytes[]` calldata slice, used as a placeholder on decode failure.
325+ function _emptyBytesArray () private pure returns (bytes [] calldata result ) {
326+ assembly ("memory-safe" ) {
327+ result.offset := 0
328+ result.length := 0
329+ }
330+ }
259331}
0 commit comments