[llvm] [Support] Error if SocketPath is too long (PR #148903)

Marina Taylor via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 15 10:19:58 PDT 2025


https://github.com/citymarina created https://github.com/llvm/llvm-project/pull/148903

If the path is longer than sockaddr_un's buffer, it will be truncated, at which point it may become indistinguishable from similar truncated paths. This will cause `bind` to fail with an "Address already in use" error. There is some existing code that checks `fs::exists` to catch these errors, but since `fs::exists` compares the full un-truncated paths, a too-long path will prevent those checks from working.

rdar://154397133

>From fe157c96324ea2a3eca1f1d0110955d0d859f0ca Mon Sep 17 00:00:00 2001
From: Marina Taylor <marina_taylor at apple.com>
Date: Tue, 15 Jul 2025 17:09:22 +0100
Subject: [PATCH] [Support] Error if SocketPath is too long

If the path is longer than sockaddr_un's buffer, it will be truncated, at which point it may become indistinguishable from similar truncated paths. This will cause `bind` to fail with an "Address already in use" error. There is some existing code that checks `fs::exists` to catch these errors, but since `fs::exists` compares the full un-truncated paths, a too-long path will prevent those checks from working.

rdar://154397133
---
 llvm/lib/Support/raw_socket_stream.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/llvm/lib/Support/raw_socket_stream.cpp b/llvm/lib/Support/raw_socket_stream.cpp
index fd1c681672138..2296bdba77c87 100644
--- a/llvm/lib/Support/raw_socket_stream.cpp
+++ b/llvm/lib/Support/raw_socket_stream.cpp
@@ -119,6 +119,14 @@ ListeningSocket::ListeningSocket(ListeningSocket &&LS)
 Expected<ListeningSocket> ListeningSocket::createUnix(StringRef SocketPath,
                                                       int MaxBacklog) {
 
+  // If SocketPath is too long, the path will be truncated, and there may be
+  // collisions with other truncated addresses that the fs::exists check below
+  // will be unable to detect.
+  if (SocketPath.size() >= sizeof((struct sockaddr_un *)NULL)->sun_path)
+    return llvm::make_error<StringError>(
+        std::make_error_code(std::errc::filename_too_long),
+        "SocketPath too long");
+
   // Handle instances where the target socket address already exists and
   // differentiate between a preexisting file with and without a bound socket
   //



More information about the llvm-commits mailing list