From 2d524f3186a0676f0f318caefaf96486b0b226a7 Mon Sep 17 00:00:00 2001 From: Dmitry <98899785+mdqst@users.noreply.github.com> Date: Tue, 26 Nov 2024 11:53:00 +0300 Subject: [PATCH] Fix Incorrect String Length Update in toString(int256) for Negative Numbers in LibString.sol Resolved an issue in the toString(int256) function where the string length was updated at an incorrect memory location due to a pointer shift. This fix ensures proper handling of negative numbers and prevents potential memory corruption. --- src/utils/LibString.sol | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/utils/LibString.sol b/src/utils/LibString.sol index 97c89e0b..8653c315 100644 --- a/src/utils/LibString.sol +++ b/src/utils/LibString.sol @@ -17,11 +17,10 @@ library LibString { // and write the string from right to left in toString(uint256), // and thus can be sure that sub(str, 1) is an unused memory location. - let length := mload(str) // Load the string length. - // Put the - character at the start of the string contents. - mstore(str, 45) // 45 is the ASCII code for the - character. + let originalStr := str // Save the original pointer before shifting. str := sub(str, 1) // Move back the string pointer by a byte. - mstore(str, add(length, 1)) // Update the string length. + mstore(originalStr, add(length, 1)) // Update the string length at the original pointer. + } } }