[llvm] 26e1df2 - [llvm][SupportTests] Fix a race condition in temporary socket construction (#190404)

via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 3 14:40:43 PDT 2026


Author: Jon Roelofs
Date: 2026-04-03T21:40:38Z
New Revision: 26e1df20f8a0b5fe0a967c345f6316bd794b9729

URL: https://github.com/llvm/llvm-project/commit/26e1df20f8a0b5fe0a967c345f6316bd794b9729
DIFF: https://github.com/llvm/llvm-project/commit/26e1df20f8a0b5fe0a967c345f6316bd794b9729.diff

LOG: [llvm][SupportTests] Fix a race condition in temporary socket construction (#190404)

createUniquePath doesn't make an exclusive lock on the filename until
the socket is created, and thus the removal step in these tests was
creating a TOCTOU race. Instead, arrange for the file to be cleaned up
*after* we're done with it.

rdar://142847430

Added: 
    

Modified: 
    llvm/unittests/Support/raw_socket_stream_test.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Support/raw_socket_stream_test.cpp b/llvm/unittests/Support/raw_socket_stream_test.cpp
index fd4d7f1e28030..183a384339268 100644
--- a/llvm/unittests/Support/raw_socket_stream_test.cpp
+++ b/llvm/unittests/Support/raw_socket_stream_test.cpp
@@ -1,3 +1,4 @@
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Config/llvm-config.h"
 #include "llvm/Support/Casting.h"
@@ -34,9 +35,7 @@ TEST(raw_socket_streamTest, CLIENT_TO_SERVER_AND_SERVER_TO_CLIENT) {
   SmallString<100> SocketPath;
   llvm::sys::fs::createUniquePath("client_server_comms-%%%%%%.sock", SocketPath,
                                   true);
-
-  // Make sure socket file does not exist. May still be there from the last test
-  std::remove(SocketPath.c_str());
+  auto Cleanup = llvm::scope_exit([&] { std::remove(SocketPath.c_str()); });
 
   Expected<ListeningSocket> MaybeServerListener =
       ListeningSocket::createUnix(SocketPath);
@@ -76,9 +75,7 @@ TEST(raw_socket_streamTest, READ_WITH_TIMEOUT) {
   SmallString<100> SocketPath;
   llvm::sys::fs::createUniquePath("read_with_timeout-%%%%%%.sock", SocketPath,
                                   true);
-
-  // Make sure socket file does not exist. May still be there from the last test
-  std::remove(SocketPath.c_str());
+  auto Cleanup = llvm::scope_exit([&] { std::remove(SocketPath.c_str()); });
 
   Expected<ListeningSocket> MaybeServerListener =
       ListeningSocket::createUnix(SocketPath);
@@ -109,9 +106,7 @@ TEST(raw_socket_streamTest, ACCEPT_WITH_TIMEOUT) {
   SmallString<100> SocketPath;
   llvm::sys::fs::createUniquePath("accept_with_timeout-%%%%%%.sock", SocketPath,
                                   true);
-
-  // Make sure socket file does not exist. May still be there from the last test
-  std::remove(SocketPath.c_str());
+  auto Cleanup = llvm::scope_exit([&] { std::remove(SocketPath.c_str()); });
 
   Expected<ListeningSocket> MaybeServerListener =
       ListeningSocket::createUnix(SocketPath);
@@ -131,9 +126,7 @@ TEST(raw_socket_streamTest, ACCEPT_WITH_SHUTDOWN) {
   SmallString<100> SocketPath;
   llvm::sys::fs::createUniquePath("accept_with_shutdown-%%%%%%.sock",
                                   SocketPath, true);
-
-  // Make sure socket file does not exist. May still be there from the last test
-  std::remove(SocketPath.c_str());
+  auto Cleanup = llvm::scope_exit([&] { std::remove(SocketPath.c_str()); });
 
   Expected<ListeningSocket> MaybeServerListener =
       ListeningSocket::createUnix(SocketPath);


        


More information about the llvm-commits mailing list