From ede71ca3b07cba51e35862114618895a32bd93f6 Mon Sep 17 00:00:00 2001 From: Victor Zhang <111778801+victzhan@users.noreply.github.com> Date: Tue, 25 Nov 2025 17:19:27 -0500 Subject: [PATCH] =?UTF-8?q?SWDEV-567829=20-=20populateFormatStringHashMap:?= =?UTF-8?q?=20relax=20printf=20hash=20collisi=E2=80=A6=20(#1944)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * SWDEV-567829 - populateFormatStringHashMap: relax printf hash collision check for duplicate format strings * function optimized by ai --- projects/clr/rocclr/device/devhcprintf.cpp | 25 ++++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/projects/clr/rocclr/device/devhcprintf.cpp b/projects/clr/rocclr/device/devhcprintf.cpp index 95a9c43ad6..f3c505ab67 100644 --- a/projects/clr/rocclr/device/devhcprintf.cpp +++ b/projects/clr/rocclr/device/devhcprintf.cpp @@ -256,17 +256,28 @@ void handlePrintf(uint64_t* output, const uint64_t* input, uint64_t len) { // delimited by character ','. bool populateFormatStringHashMap(const std::vector& printfInfo, std::map& strMap) { - for (auto it : printfInfo) { - auto Delim = it.fmtString_.find_first_of(','); - auto HashStr = it.fmtString_.substr(0, Delim); + static_assert(sizeof(long long) == sizeof(uint64_t), "unexpected long long type width"); - static_assert(sizeof(long long) == sizeof(uint64_t), "unexpected long long type width"); - auto HashVal = std::strtoull(HashStr.c_str(), NULL, 16); - if (strMap.find(HashVal) != strMap.end()) { + for (const auto& info : printfInfo) { + auto Delim = info.fmtString_.find(','); + if (Delim == std::string::npos) { + LogError("Missing delimiter in printf metadata"); + return false; + } + + const char* HashStr = info.fmtString_.c_str(); + char* ParseEnd = nullptr; + auto HashVal = std::strtoull(HashStr, &ParseEnd, 16); + if (ParseEnd != HashStr + Delim) { + LogError("Failed to parse printf hash"); + return false; + } + + auto InsertResult = strMap.emplace(HashVal, info.fmtString_.substr(Delim + 1)); + if (!InsertResult.second) { LogError("Hash value collision detected, printf buffer ill formed"); return false; } - strMap[HashVal] = it.fmtString_.substr(Delim + 1, it.fmtString_.size()); } return true;