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

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 18 13:51:06 PST 2024


================
@@ -894,18 +894,27 @@ 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. */
-  if (PNS < OldPNS)
+  if (PNS < OldPNS) {
+    if (OldFilenamePat) {
+      free(OldFilenamePat);
+      OldFilenamePat = NULL;
----------------
mstorsjo wrote:

I don't quite see the need for setting the variable to NULL right before returning - it's a local variable after all? Plus you don't need to check whether it's non-null before invoking `free()`, it's ok to call `free(NULL)`. So it should be enough with just one line with `free(OldFilenamePat);` in each codepath.

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


More information about the llvm-commits mailing list