The _quantAMMPack128Array
function in the QuantAMMStorage
contract contains a potential precision loss issue when casting an int256
value to int128
and back to int256
. This can lead to incorrect values being stored, which may affect the contract's financial calculations and state integrity.
Line of Affected Code:
This issue occurs in the _quantAMMPack128Array
function when handling an array with an odd length. The last element of the _sourceArray
is cast to int128
and then back to int256
.
The original value, _sourceArray[sourceArrayLength - 1]
, is of type int256
. When cast to int128
, any value outside the range of int128
(-2^127 to 2^127 - 1) will be truncated. This truncation results in a loss of precision, and the value is then cast back to int256
, but the original value cannot be recovered.
What Is the Effect:
The precision loss can lead to data corruption, fund loss, incorrect contract state, and potential exploitability. If the value represents a financial metric, truncation could lead to significant discrepancies, affecting the contract's operations and security.
The precision loss issue can have a high impact on the contract's functionality and security. It can lead to fund loss, incorrect state representation, and potential exploitation by attackers. The issue is particularly severe because once precision is lost, it cannot be recovered, leading to permanent errors in the contract's operation.
Manual
Range Checks: Implement range checks to ensure that values fit within the int128
range before casting. This can prevent truncation and precision loss.
Avoid Unnecessary Casting: Where possible, avoid casting between different integer sizes, especially for financial values.
Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelyhood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.