[compiler-rt] [compiler-rt][profile] Duplicate filename in `parseAndSetFilename` if exists (PR #110264)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 27 06:14:02 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-pgo

Author: Antonio Frighetto (antoniofrighetto)

<details>
<summary>Changes</summary>

Duplicate `lprofCurFilename.FilenamePat` if the filename exists in `parseAndSetFilename`, as the original path name buffer may have been freed by the time it is used.

Fixes: https://github.com/llvm/llvm-project/issues/102109.

---
Full diff: https://github.com/llvm/llvm-project/pull/110264.diff


1 Files Affected:

- (modified) compiler-rt/lib/profile/InstrProfilingFile.c (+6-1) 


``````````diff
diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c
index 64ed8b62e9eba7..8243a7a5ea3ca9 100644
--- a/compiler-rt/lib/profile/InstrProfilingFile.c
+++ b/compiler-rt/lib/profile/InstrProfilingFile.c
@@ -894,7 +894,9 @@ static void parseAndSetFilename(const char *FilenamePat,
                                 ProfileNameSpecifier PNS,
                                 unsigned CopyFilenamePat) {
 
-  const char *OldFilenamePat = lprofCurFilename.FilenamePat;
+  char *OldFilenamePat = lprofCurFilename.FilenamePat
+                             ? strdup(lprofCurFilename.FilenamePat)
+                             : NULL;
   ProfileNameSpecifier OldPNS = lprofCurFilename.PNS;
 
   /* The old profile name specifier takes precedence over the old one. */
@@ -923,6 +925,9 @@ static void parseAndSetFilename(const char *FilenamePat,
       PROF_NOTE("Override old profile path \"%s\" via %s to \"%s\" via %s.\n",
                 OldFilenamePat, getPNSStr(OldPNS), lprofCurFilename.FilenamePat,
                 getPNSStr(PNS));
+
+    free(OldFilenamePat);
+    OldFilenamePat = NULL;
   }
 
   truncateCurrentFile();

``````````

</details>


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


More information about the llvm-commits mailing list