[compiler-rt] r184895 - Fix a use after free I introduced and that Bill caught in code review

Chandler Carruth chandlerc at gmail.com
Tue Jun 25 17:26:16 PDT 2013


Author: chandlerc
Date: Tue Jun 25 19:26:16 2013
New Revision: 184895

URL: http://llvm.org/viewvc/llvm-project?rev=184895&view=rev
Log:
Fix a use after free I introduced and that Bill caught in code review
(thanks!) by deferring the free of the filename until we finish writing
the coverage data to that file.

Bill, let me know if you'd prefer a different approach!

Modified:
    compiler-rt/trunk/lib/profile/GCDAProfiling.c

Modified: compiler-rt/trunk/lib/profile/GCDAProfiling.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/GCDAProfiling.c?rev=184895&r1=184894&r2=184895&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/GCDAProfiling.c (original)
+++ compiler-rt/trunk/lib/profile/GCDAProfiling.c Tue Jun 25 19:26:16 2013
@@ -271,7 +271,6 @@ void llvm_gcda_start_file(const char *or
         fprintf(stderr, "profiling: %s: cannot open: %s\n", filename,
                 strerror(errnum));
 #endif
-        free(filename);
         return;
       }
     }
@@ -303,8 +302,6 @@ void llvm_gcda_start_file(const char *or
   write_bytes(version, 4);
   write_bytes("MVLL", 4);
 
-  free(filename);
-
 #ifdef DEBUG_GCDAPROFILING
   fprintf(stderr, "llvmgcda: [%s]\n", orig_filename);
 #endif
@@ -409,19 +406,21 @@ void llvm_gcda_emit_arcs(uint32_t num_co
 
 void llvm_gcda_end_file() {
   /* Write out EOF record. */
-  if (!output_file) return;
-  write_bytes("\0\0\0\0\0\0\0\0", 8);
+  if (output_file) {
+    write_bytes("\0\0\0\0\0\0\0\0", 8);
 
-  if (new_file) {
-    fwrite(write_buffer, cur_pos, 1, output_file);
-    free(write_buffer);
-  } else {
-    unmap_file();
-  }
+    if (new_file) {
+      fwrite(write_buffer, cur_pos, 1, output_file);
+      free(write_buffer);
+    } else {
+      unmap_file();
+    }
 
-  fclose(output_file);
-  output_file = NULL;
-  write_buffer = NULL;
+    fclose(output_file);
+    output_file = NULL;
+    write_buffer = NULL;
+  }
+  free(filename);
 
 #ifdef DEBUG_GCDAPROFILING
   fprintf(stderr, "llvmgcda: -----\n");





More information about the llvm-commits mailing list