Since web3 apparently doesn't require functions with uint
parameters to have positive values for those parameters, I thought I'd try it in Solidity. I'm posting this to see if I'm overthinking it, if there is any unnecessary code, or if there's still a way it could break. For the contract I'm working on, this would be infrequently used but important so it would be worth making sure that negative or irregular amounts would never be set.
modifier validateUintFromInt(int _value) {
require(_value > 0); // check positive value
require(_value <= 2**128 - 1); // assumes nothing more than uint128 is needed - not sure if this is necessary with the statement below
require(_value == int(uint(_value))); // uint and int overflow at different points so this would provide some assurance that massive numbers would not be allowed
_;
}
This could be used like this:
function updateValue(int _newInt) external validateUintFromInt(_newInt) {
uint _newUint = uint(_newInt);
globalUint = _newUint;
}
uint256
as a parameter?-1
would be the same as accidentally passing2**256-1
. If2**256-1
is out of bounds, just accept auint256
and dorequire(value <= MAX_VALUE, "Number out of range.");