KSA-01C: Ineffectual Usage of Safe Arithmetics
Description:
The linked mathematical operation is guaranteed to be performed safely by logical inference, such as surrounding conditionals evaluated in require checks or if-else constructs.
Example:
uint256 gasBefore = gasleft();
if (block.timestamp > params.deadline) {
revert DeadlinePassed(params.deadline, block.timestamp);
}
uint256[] memory inputBalances = _recordInputBalances(params.inputTokens);
uint256[] memory outputBalances =
_recordOutputBalances(params.outputTokens, params.outputData, params.recipient);
uint256 nativeBalanceBefore = address(this).balance - msg.value;
_callPermit2(params.permit2Data);
_collectInputTokens(params.inputTokens, params.inputAmounts, params.inputData);
_callExecutor(params.executor, address(this).balance - nativeBalanceBefore, params.executorData);
outputAmounts =
_processOutputTokens(params.outputTokens, params.outputData, outputBalances, params.recipient);
_refundInputTokens(params.inputTokens, inputBalances, params.recipient);
emit Swap(
msg.sender,
params.executor,
params.recipient,
params.inputTokens,
params.inputAmounts,
params.outputTokens,
outputAmounts
);
emit ClientData(params.clientData);
gasUsed = gasBefore - gasleft();
Recommendation:
Given that safe arithmetics are toggled on by default in pragma versions of 0.8.X, we advise the linked statement to be wrapped in an unchecked code block thereby optimizing its execution cost.
KSA-01C: Ineffectual Usage of Safe Arithmetics
• I-1: L93
• I-2: L335
Description:
The linked mathematical operation is guaranteed to be performed safely by logical inference, such as surrounding conditionals evaluated in
requirechecks orif-elseconstructs.Example:
Recommendation:
Given that safe arithmetics are toggled on by default in
pragmaversions of0.8.X, we advise the linked statement to be wrapped in anuncheckedcode block thereby optimizing its execution cost.