From 25b649baf535c7d92690b917ffeea11af2eff6ab Mon Sep 17 00:00:00 2001 From: ViharGandhi Date: Tue, 28 Mar 2023 12:03:53 +0530 Subject: [PATCH] Moved owner check from public to internal function in ERC721 contract - resolves issue #4136 Updated ERC721._approve function to include a check for the owner before approval is granted. This ensures that the owner is not accidentally approved and prevents unnecessary sload calls. Moved the check from the public ERC721.approve function to the internal ERC721._approve function. Resolves issue #4136. --- contracts/token/ERC721/ERC721.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contracts/token/ERC721/ERC721.sol b/contracts/token/ERC721/ERC721.sol index 428338d900c..1b5830802d1 100644 --- a/contracts/token/ERC721/ERC721.sol +++ b/contracts/token/ERC721/ERC721.sol @@ -111,7 +111,7 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata { */ function approve(address to, uint256 tokenId) public virtual override { address owner = ERC721.ownerOf(tokenId); - require(to != owner, "ERC721: approval to current owner"); + require( _msgSender() == owner || isApprovedForAll(owner, _msgSender()), @@ -364,6 +364,7 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata { * Emits an {Approval} event. */ function _approve(address to, uint256 tokenId) internal virtual { + require(to != owner, "ERC721: approval to current owner"); _tokenApprovals[tokenId] = to; emit Approval(ERC721.ownerOf(tokenId), to, tokenId); }