[Lldb-commits] [lldb] [lldb][Windows] Open paths longer than MAX_PATH in FileSystem::Open (PR #229727)

Charles Zablit via lldb-commits lldb-commits at lists.llvm.org
Wed Oct 7 05:27:04 PDT 2026


https://github.com/charles-zablit updated https://github.com/llvm/llvm-project/pull/229727

>From 72c6dde22837ee4dcdd0b03ac7b3fc6971813625 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Wed, 7 Oct 2026 12:16:55 +0100
Subject: [PATCH 1/2] [lldb][Windows] Open paths longer than MAX_PATH in
 FileSystem::Open

---
 lldb/source/Host/windows/FileSystem.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lldb/source/Host/windows/FileSystem.cpp b/lldb/source/Host/windows/FileSystem.cpp
index f070364151b3f..49961f9ec3a48 100644
--- a/lldb/source/Host/windows/FileSystem.cpp
+++ b/lldb/source/Host/windows/FileSystem.cpp
@@ -20,6 +20,7 @@
 
 #include "llvm/Support/ConvertUTF.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Windows/WindowsSupport.h"
 
 using namespace lldb_private;
 
@@ -100,12 +101,12 @@ FILE *FileSystem::Fopen(const char *path, const char *mode) {
 }
 
 int FileSystem::Open(const char *path, int flags, int mode) {
-  std::wstring wpath;
-  if (!llvm::ConvertUTF8toWide(path, wpath))
+  llvm::SmallVector<wchar_t, MAX_PATH> wpath;
+  if (llvm::sys::windows::widenPath(path, wpath))
     return -1;
   // All other bits are rejected by _wsopen_s
   mode = mode & (_S_IREAD | _S_IWRITE);
   int result;
-  ::_wsopen_s(&result, wpath.c_str(), flags, _SH_DENYNO, mode);
+  ::_wsopen_s(&result, wpath.data(), flags, _SH_DENYNO, mode);
   return result;
 }

>From 223988beb0ee2c2a23b21b8bd7bb58fa87fca2f2 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Wed, 7 Oct 2026 13:26:45 +0100
Subject: [PATCH 2/2] fixup! [lldb][Windows] Open paths longer than MAX_PATH in
 FileSystem::Open

---
 lldb/source/Host/windows/FileSystem.cpp | 32 +++++++++++++++----------
 1 file changed, 20 insertions(+), 12 deletions(-)

diff --git a/lldb/source/Host/windows/FileSystem.cpp b/lldb/source/Host/windows/FileSystem.cpp
index 49961f9ec3a48..686ab3a6cafe6 100644
--- a/lldb/source/Host/windows/FileSystem.cpp
+++ b/lldb/source/Host/windows/FileSystem.cpp
@@ -31,20 +31,22 @@ const char *FileSystem::PATH_CONVERSION_ERROR =
 
 Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
   Status error;
-  std::wstring wsrc, wdst;
-  if (!llvm::ConvertUTF8toWide(src.GetPath(), wsrc) ||
+  llvm::SmallVector<wchar_t, MAX_PATH> wsrc, wdst_long;
+  std::wstring wdst;
+  if (llvm::sys::windows::widenPath(src.GetPath(), wsrc) ||
+      llvm::sys::windows::widenPath(dst.GetPath(), wdst_long) ||
       !llvm::ConvertUTF8toWide(dst.GetPath(), wdst))
     error = Status::FromErrorString(PATH_CONVERSION_ERROR);
   if (error.Fail())
     return error;
-  DWORD attrib = ::GetFileAttributesW(wdst.c_str());
+  DWORD attrib = ::GetFileAttributesW(wdst_long.data());
   if (attrib == INVALID_FILE_ATTRIBUTES) {
     error = Status(::GetLastError(), lldb::eErrorTypeWin32);
     return error;
   }
   bool is_directory = !!(attrib & FILE_ATTRIBUTE_DIRECTORY);
   DWORD flag = is_directory ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0;
-  BOOL result = ::CreateSymbolicLinkW(wsrc.c_str(), wdst.c_str(), flag);
+  BOOL result = ::CreateSymbolicLinkW(wsrc.data(), wdst.c_str(), flag);
   if (!result)
     error = Status(::GetLastError(), lldb::eErrorTypeWin32);
   return error;
@@ -52,29 +54,34 @@ Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
 
 Status FileSystem::Readlink(const FileSpec &src, FileSpec &dst) {
   Status error;
-  std::wstring wsrc;
-  if (!llvm::ConvertUTF8toWide(src.GetPath(), wsrc)) {
+  llvm::SmallVector<wchar_t, MAX_PATH> wsrc;
+  if (llvm::sys::windows::widenPath(src.GetPath(), wsrc)) {
     error = Status::FromErrorString(PATH_CONVERSION_ERROR);
     return error;
   }
 
   HANDLE h = ::CreateFileW(
-      wsrc.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
+      wsrc.data(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
       OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
   if (h == INVALID_HANDLE_VALUE) {
     error = Status(::GetLastError(), lldb::eErrorTypeWin32);
     return error;
   }
 
-  std::vector<wchar_t> buf(PATH_MAX + 1);
+  llvm::SmallVector<wchar_t, MAX_PATH> buf(MAX_PATH + 1);
   // Subtract 1 from the path length since this function does not add a null
   // terminator. The result is an extended-length ("\\?\") path.
   DWORD result = ::GetFinalPathNameByHandleW(
       h, buf.data(), buf.size() - 1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
+  while (result >= buf.size() - 1) {
+    buf.resize(result + 1);
+    result = ::GetFinalPathNameByHandleW(
+        h, buf.data(), buf.size() - 1, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
+  }
   std::string path;
   if (result == 0)
     error = Status(::GetLastError(), lldb::eErrorTypeWin32);
-  else if (!llvm::convertWideToUTF8(buf.data(), path))
+  else if (!llvm::convertWideToUTF8(std::wstring(buf.data(), result), path))
     error = Status::FromErrorString(PATH_CONVERSION_ERROR);
   else
     dst.SetFile(StripExtendedLengthPrefix(path), FileSpec::Style::native);
@@ -89,13 +96,14 @@ Status FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) {
 }
 
 FILE *FileSystem::Fopen(const char *path, const char *mode) {
-  std::wstring wpath, wmode;
-  if (!llvm::ConvertUTF8toWide(path, wpath))
+  llvm::SmallVector<wchar_t, MAX_PATH> wpath;
+  std::wstring wmode;
+  if (llvm::sys::windows::widenPath(path, wpath))
     return nullptr;
   if (!llvm::ConvertUTF8toWide(mode, wmode))
     return nullptr;
   FILE *file;
-  if (_wfopen_s(&file, wpath.c_str(), wmode.c_str()) != 0)
+  if (_wfopen_s(&file, wpath.data(), wmode.c_str()) != 0)
     return nullptr;
   return file;
 }



More information about the lldb-commits mailing list