Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SafeMath to avoid memory leaks #2462

Merged
merged 16 commits into from
Jan 18, 2021

Conversation

Amxx
Copy link
Collaborator

@Amxx Amxx commented Jan 8, 2021

FIxes #2413, remove the string allocation in SafeMath default functions: caused unrecovered memory allocation even for successfull calls

FIxes OpenZeppelin#2413, remove the string allocation in SafeMath  default functions: caused unrecovered memory allocation even for successfull calls
@frangio
Copy link
Contributor

frangio commented Jan 11, 2021

After discussing offline we agreed on the following:

  • We will deprecate the existing functions like sub(uint, uint, string) because they leak memory. (This is adding a comment in the source code and in the release notes.)
  • We will implement sub(uint, uint) by directly calling require so it does not leak memory.
  • We will add a new family of functions that return a pair (bool, uint) that can be used to protect against overflow with custom revert reasons, and use these internally in the library. The name for these functions is TBD.

(This also implies that the new add(uint, uint, string) and mul(uint, uint, string) functions added in this PR should be removed.)

Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to add the same deprecation notice for EnumerableMap.get. We use it in ERC721.ownerOf so while we're at it we should probably change that too.

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
contracts/math/SafeMath.sol Outdated Show resolved Hide resolved
contracts/math/SafeMath.sol Outdated Show resolved Hide resolved
contracts/math/SafeMath.sol Outdated Show resolved Hide resolved
contracts/math/SafeMath.sol Show resolved Hide resolved
test/math/SafeMath.test.js Outdated Show resolved Hide resolved
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work thanks!

@frangio frangio merged commit c342114 into OpenZeppelin:master Jan 18, 2021
@Amxx Amxx deleted the fix/safemathmemory branch January 18, 2021 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The SafeMath.div function needlessly allocates memory
2 participants