Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NEAR and safe math on unsigned integers

Tags:

nearprotocol

On Ethereum Solidity, a special library called SafeMath needs to be used when dealing with unsigned integer balance number. This is because of the integer overflow exploits.

Does NEAR smart contracts written in Rust need similar mitigations? Or does Rust trap the oveflow automatically and panic?

like image 602
Mikko Ohtamaa Avatar asked Nov 26 '25 09:11

Mikko Ohtamaa


1 Answers

By default, Rust has overflow checks enabled for debug builds, but disabled in optimized release builds. You can easily tweak it in Cargo.toml by setting overflow-checks in profile.release section:

[profile.release]
# ...
overflow-checks = true

NEAR core contracts opt-into the paranoid mode.

Even if you choose to use saturating_* or checked_* methods explicitly, extra checks are still recommended.

like image 133
Vlad Frolov Avatar answered Nov 28 '25 16:11

Vlad Frolov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!