From 9c7f61cebc19b128e450648eb064589531dc727c Mon Sep 17 00:00:00 2001 From: roby65 Date: Thu, 26 Feb 2026 01:08:51 +0100 Subject: [PATCH 1/3] Fix error when filename is too long --- ps2xRecomp/include/ps2recomp/ps2_recompiler.h | 1 + ps2xRecomp/src/lib/ps2_recompiler.cpp | 39 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h index b327ddad..23f25dfd 100644 --- a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h +++ b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h @@ -70,6 +70,7 @@ namespace ps2recomp bool generateStubHeader(); bool writeToFile(const std::string &path, const std::string &content); std::filesystem::path getOutputPath(const Function &function) const; + std::string PS2Recompiler::clampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength) const; std::string sanitizeFunctionName(const std::string &name) const; }; diff --git a/ps2xRecomp/src/lib/ps2_recompiler.cpp b/ps2xRecomp/src/lib/ps2_recompiler.cpp index a85cb2a9..116ea034 100644 --- a/ps2xRecomp/src/lib/ps2_recompiler.cpp +++ b/ps2xRecomp/src/lib/ps2_recompiler.cpp @@ -1451,11 +1451,48 @@ namespace ps2recomp } std::filesystem::path outputPath = m_config.outputPath; - outputPath /= safeName + ".cpp"; + outputPath /= clampFilenameLength(safeName, ".cpp", 100); return outputPath; } + std::string PS2Recompiler::clampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength) const + { + if (maxLength == 0) + { + std::cerr << "clampFilenameLength::maxLength must be greater than 0" << std::endl; + //Better go over the limit than create files with an empty path + return baseName + extension; + } + + if (baseName.size() + extension.size() <= maxLength) + return baseName + extension; + + std::string namePart = baseName; + std::string preservedSuffix; + + auto suffixPos = namePart.rfind("_0x"); + if (suffixPos != std::string::npos) + { + preservedSuffix = namePart.substr(suffixPos); + namePart = namePart.substr(0, suffixPos); + } + + std::size_t available = maxLength - extension.size() - preservedSuffix.size(); + + if (available == 0) + { + return preservedSuffix + extension; + } + + if (namePart.size() > available) + namePart = namePart.substr(0, available); + + std::cout << baseName << " filename will be truncated to " << namePart << " because is more than " << maxLength << " characters (" << baseName.length() << " characters)" << std::endl; + + return namePart + preservedSuffix + extension; + } + std::string PS2Recompiler::sanitizeFunctionName(const std::string &name) const { std::string sanitized = sanitizeIdentifierBody(name); From 5bb9d8f94ca791e0c045e97920156ba9aa73a0b3 Mon Sep 17 00:00:00 2001 From: roby65 <1843702+roby65@users.noreply.github.com> Date: Thu, 26 Feb 2026 07:41:09 +0100 Subject: [PATCH 2/3] Fix typo --- ps2xRecomp/include/ps2recomp/ps2_recompiler.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h index 23f25dfd..39ddd5bd 100644 --- a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h +++ b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h @@ -70,7 +70,7 @@ namespace ps2recomp bool generateStubHeader(); bool writeToFile(const std::string &path, const std::string &content); std::filesystem::path getOutputPath(const Function &function) const; - std::string PS2Recompiler::clampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength) const; + std::string clampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength) const; std::string sanitizeFunctionName(const std::string &name) const; }; From 0d718f5c202af551f51f7970ad6403c9e8c7b150 Mon Sep 17 00:00:00 2001 From: roby65 <1843702+roby65@users.noreply.github.com> Date: Fri, 27 Feb 2026 23:15:43 +0100 Subject: [PATCH 3/3] Added tests --- ps2xRecomp/include/ps2recomp/ps2_recompiler.h | 4 +++- ps2xRecomp/src/lib/ps2_recompiler.cpp | 9 ++++++--- ps2xTest/src/ps2_recompiler_tests.cpp | 11 ++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h index 39ddd5bd..10c9f5c2 100644 --- a/ps2xRecomp/include/ps2recomp/ps2_recompiler.h +++ b/ps2xRecomp/include/ps2recomp/ps2_recompiler.h @@ -40,6 +40,8 @@ namespace ps2recomp std::vector &functions, std::unordered_map> &decodedFunctions); + static std::string ClampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength); + private: ConfigManager m_configManager; std::unique_ptr m_elfParser; @@ -70,7 +72,7 @@ namespace ps2recomp bool generateStubHeader(); bool writeToFile(const std::string &path, const std::string &content); std::filesystem::path getOutputPath(const Function &function) const; - std::string clampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength) const; + static std::string clampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength); std::string sanitizeFunctionName(const std::string &name) const; }; diff --git a/ps2xRecomp/src/lib/ps2_recompiler.cpp b/ps2xRecomp/src/lib/ps2_recompiler.cpp index 116ea034..01c50820 100644 --- a/ps2xRecomp/src/lib/ps2_recompiler.cpp +++ b/ps2xRecomp/src/lib/ps2_recompiler.cpp @@ -1456,7 +1456,7 @@ namespace ps2recomp return outputPath; } - std::string PS2Recompiler::clampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength) const + std::string PS2Recompiler::clampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength) { if (maxLength == 0) { @@ -1488,8 +1488,6 @@ namespace ps2recomp if (namePart.size() > available) namePart = namePart.substr(0, available); - std::cout << baseName << " filename will be truncated to " << namePart << " because is more than " << maxLength << " characters (" << baseName.length() << " characters)" << std::endl; - return namePart + preservedSuffix + extension; } @@ -1547,4 +1545,9 @@ namespace ps2recomp } return StubTarget::Unknown; } + + std::string PS2Recompiler::ClampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength) + { + return clampFilenameLength(baseName, extension, maxLength); + } } diff --git a/ps2xTest/src/ps2_recompiler_tests.cpp b/ps2xTest/src/ps2_recompiler_tests.cpp index 575c647c..d2fece95 100644 --- a/ps2xTest/src/ps2_recompiler_tests.cpp +++ b/ps2xTest/src/ps2_recompiler_tests.cpp @@ -513,5 +513,14 @@ void register_ps2_recompiler_tests() std::error_code removeError; std::filesystem::remove(elfPath, removeError); - }); }); + }); + + tc.Run("respect max length for .cpp filenames", [](TestCase& t) { + + t.IsTrue(PS2Recompiler::ClampFilenameLength("ReallyLongFunctionNameReallyLongFunctionNameReallyLongFunctionName_0x12345678",".cpp",50).length() <= 50,"Function name must be max 50 characters"); + + t.IsTrue(PS2Recompiler::ClampFilenameLength("ReallyLongFunctionNameReallyLongFunctionNameReallyLongFunctionName_0x12345678", ".cpp", 50).rfind("0x12345678") != std::string::npos, "Function name must mantain the function address at the end, if present"); + + }); + }); }