The function toHexString(uint256 value, uint256 length)
in the LibStrings.sol
does not correctly handle leading zeros in hexadecimal string representations. This can result in strings that are shorter than expected, potentially leading to incorrect data representations when these strings are used to represent addresses or fixed-length hexadecimal data.
The function is designed to convert a uint256
value into a hexadecimal string of a specified length. But the current implementation constructs this string by filling a buffer from the end to the beginning, based on the least significant to the most significant digit of the value
. Albeit, if the value
has leading zeros (i.e., the most significant digits are zero), these are not represented in the resulting string, as the buffer initialization does not account for them.
Take a look at this part of the code:
https://github.com/Cyfrin/2024-05-Beanstalk-3/blob/662d26f12ee219ee92dc485c06e01a4cb5ee8dfb/protocol/contracts/libraries/LibStrings.sol#L39-L49
The loop fills the buffer with hexadecimal characters corresponding to the value
, but does not pre-fill the buffer with '0' characters. This results in uninitialized spaces in the buffer if value
does not have enough significant hexadecimal digits to fill the specified length
.
Likelihood: Medium
Impact: Medium
The impact of this issue is multifaceted and can lead to several significant problems. Firstly, there is data integrity issue, as the function may not accurately represent data that requires a fixed-length hexadecimal format, such as blockchain addresses. This misrepresentation can lead to security risks, including the possibility of transactions being mistakenly sent to the wrong addresses due to incorrect formatting. Additionally, protocols that rely on a strict length format for processing or validation might either reject the improperly formatted data or malfunction, causing interoperability issues.
Manual review
The function should be modified to ensure that the buffer is correctly initialized with '0' characters for the entire length of the expected hexadecimal string. We can do this by adding a loop to initialize the buffer before filling it with the hexadecimal digits of the value
. Like this:
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.