Skip to content

Commit 1311b36

Browse files
authored
[llvm][Support] Put back filename into FileToRemoveList (#124065)
Prevents avoidable memory leaks. Looks like exchange added in aa1333a didn't take "continue" into account. ``` ==llc==2150782==ERROR: LeakSanitizer: detected memory leaks Direct leak of 10 byte(s) in 1 object(s) allocated from: #0 0x5f1b0f9ac14a in strdup llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:593:3 #1 0x5f1b1768428d in FileToRemoveList llvm-project/llvm/lib/Support/Unix/Signals.inc:105:55 ```
1 parent 4f26edd commit 1311b36

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

llvm/lib/Support/Unix/Signals.inc

+19-15
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ public:
149149
}
150150
}
151151

152+
static void removeFile(char *path) {
153+
// Get the status so we can determine if it's a file or directory. If we
154+
// can't stat the file, ignore it.
155+
struct stat buf;
156+
if (stat(path, &buf) != 0)
157+
return;
158+
159+
// If this is not a regular file, ignore it. We want to prevent removal
160+
// of special files like /dev/null, even if the compiler is being run
161+
// with the super-user permissions.
162+
if (!S_ISREG(buf.st_mode))
163+
return;
164+
165+
// Otherwise, remove the file. We ignore any errors here as there is
166+
// nothing else we can do.
167+
unlink(path);
168+
}
169+
152170
// Signal-safe.
153171
static void removeAllFiles(std::atomic<FileToRemoveList *> &Head) {
154172
// If cleanup were to occur while we're removing files we'd have a bad time.
@@ -162,21 +180,7 @@ public:
162180
// If erasing was occuring while we're trying to remove files we'd look
163181
// at free'd data. Take away the path and put it back when done.
164182
if (char *path = currentFile->Filename.exchange(nullptr)) {
165-
// Get the status so we can determine if it's a file or directory. If we
166-
// can't stat the file, ignore it.
167-
struct stat buf;
168-
if (stat(path, &buf) != 0)
169-
continue;
170-
171-
// If this is not a regular file, ignore it. We want to prevent removal
172-
// of special files like /dev/null, even if the compiler is being run
173-
// with the super-user permissions.
174-
if (!S_ISREG(buf.st_mode))
175-
continue;
176-
177-
// Otherwise, remove the file. We ignore any errors here as there is
178-
// nothing else we can do.
179-
unlink(path);
183+
removeFile(path);
180184

181185
// We're done removing the file, erasing can safely proceed.
182186
currentFile->Filename.exchange(path);

0 commit comments

Comments
 (0)