[llvm] [SampleProfile] Fix UB in Demangler invocation. (PR #137659)

Krzysztof Pszeniczny via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 28 09:35:12 PDT 2025


================
@@ -737,14 +737,13 @@ bool SampleProfileMatcher::functionMatchesProfileHelper(
     auto FunctionName = FName.str();
     if (Demangler.partialDemangle(FunctionName.c_str()))
       return std::string();
-    constexpr size_t MaxBaseNameSize = 65536;
-    std::vector<char> BaseNameBuf(MaxBaseNameSize, 0);
-    size_t BaseNameSize = MaxBaseNameSize;
-    char *BaseNamePtr =
-        Demangler.getFunctionBaseName(BaseNameBuf.data(), &BaseNameSize);
-    return (BaseNamePtr && BaseNameSize)
-               ? std::string(BaseNamePtr, BaseNameSize)
-               : std::string();
+    size_t BaseNameSize = 0;
+    char *BaseNamePtr = Demangler.getFunctionBaseName(nullptr, &BaseNameSize);
+    std::string Result = (BaseNamePtr && BaseNameSize)
+                             ? std::string(BaseNamePtr, BaseNameSize)
+                             : std::string();
+    free(BaseNamePtr);
----------------
amharc wrote:

Done. I didn't point to the libstdc++ docs because I'm not really sure how authoritative this particular link is, but I mentioned __cxa_demangle (which is what the demangler API does itself too).

https://github.com/llvm/llvm-project/pull/137659


More information about the llvm-commits mailing list