Index: lib/profile/GCDAProfiling.c =================================================================== --- lib/profile/GCDAProfiling.c +++ lib/profile/GCDAProfiling.c @@ -352,17 +352,20 @@ /* Try opening the file, creating it if necessary. */ new_file = 1; mode = "w+b"; - fd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644); + fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0644); if (fd == -1) { /* Try creating the directories first then opening the file. */ __llvm_profile_recursive_mkdir(filename); - fd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644); + fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0644); if (fd == -1) { - /* Bah! It's hopeless. */ - int errnum = errno; - fprintf(stderr, "profiling: %s: cannot open: %s\n", filename, - strerror(errnum)); - return; + fd = open(filename, O_RDWR | O_BINARY); + if (fd == -1) { + /* Bah! It's hopeless. */ + int errnum = errno; + fprintf(stderr, "profiling: %s: cannot open: %s\n", filename, + strerror(errnum)); + return; + } } } } @@ -371,7 +374,12 @@ * same GCDA. This can fail if the filesystem doesn't support it, but in that * case we'll just carry on with the old racy behaviour and hope for the best. */ - lprofLockFd(fd); + if (lprofLockFd(fd) != 0) { + int errnum = errno; + fprintf(stderr, "profiling: %s: cannot lock: %s\n", filename, + strerror(errnum)); + return; + } output_file = fdopen(fd, mode); /* Initialize the write buffer. */ Index: lib/profile/InstrProfilingUtil.c =================================================================== --- lib/profile/InstrProfilingUtil.c +++ lib/profile/InstrProfilingUtil.c @@ -125,8 +125,7 @@ } return 0; #else - flock(fd, LOCK_EX); - return 0; + return flock(fd, LOCK_EX); #endif } @@ -150,8 +149,7 @@ } return 0; #else - flock(fd, LOCK_UN); - return 0; + return flock(fd, LOCK_UN); #endif }