Author: Charles Zablit
Date: 2026-05-05T17:14:16+01:00
New Revision: 176dff4a5afdaa7f8ca040b4e06c96ef918e8a22

URL: 
https://github.com/llvm/llvm-project/commit/176dff4a5afdaa7f8ca040b4e06c96ef918e8a22
DIFF: 
https://github.com/llvm/llvm-project/commit/176dff4a5afdaa7f8ca040b4e06c96ef918e8a22.diff

LOG: [lldb][windows] fix cross DLL file descriptor lookup crash (#195855)

On Windows, file descriptors are only valid in the same DLL: they are
really just handles mapped to an index in a table in the CRT. Calling a
liblldb method with a file descriptor from lldb-dap will cause the
program to crash. See
https://github.com/llvm/llvm-project/issues/193971.

This patch fixes the issue by refactoring the `NativeFile` constructors
so that they no longer try to convert `FILE` types to handles through
the CRT lookup table.

Added: 
    

Modified: 
    lldb/source/Host/common/File.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Host/common/File.cpp 
b/lldb/source/Host/common/File.cpp
index 8346cc67ca445..9ffdf014a8bdb 100644
--- a/lldb/source/Host/common/File.cpp
+++ b/lldb/source/Host/common/File.cpp
@@ -255,9 +255,17 @@ NativeFile::NativeFile(FILE *fh, OpenOptions options, bool 
transfer_ownership)
   // In order to properly display non ASCII characters in Windows, we need to
   // use Windows APIs to print to the console. This is only required if the
   // stream outputs to a console.
-  int fd = _fileno(fh);
-  is_windows_console =
-      ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
+  {
+    HANDLE h = INVALID_HANDLE_VALUE;
+    if (fh == stdin)
+      h = ::GetStdHandle(STD_INPUT_HANDLE);
+    else if (fh == stdout)
+      h = ::GetStdHandle(STD_OUTPUT_HANDLE);
+    else if (fh == stderr)
+      h = ::GetStdHandle(STD_ERROR_HANDLE);
+    is_windows_console =
+        h != INVALID_HANDLE_VALUE && ::GetFileType(h) == FILE_TYPE_CHAR;
+  }
 #else
 #ifndef NDEBUG
   int fd = fileno(fh);
@@ -288,8 +296,17 @@ NativeFile::NativeFile(int fd, OpenOptions options, bool 
transfer_ownership)
   // In order to properly display non ASCII characters in Windows, we need to
   // use Windows APIs to print to the console. This is only required if the
   // file outputs to a console.
-  is_windows_console =
-      ::GetFileType((HANDLE)::_get_osfhandle(fd)) == FILE_TYPE_CHAR;
+  {
+    HANDLE h = INVALID_HANDLE_VALUE;
+    if (fd == STDIN_FILENO)
+      h = ::GetStdHandle(STD_INPUT_HANDLE);
+    else if (fd == STDOUT_FILENO)
+      h = ::GetStdHandle(STD_OUTPUT_HANDLE);
+    else if (fd == STDERR_FILENO)
+      h = ::GetStdHandle(STD_ERROR_HANDLE);
+    is_windows_console =
+        h != INVALID_HANDLE_VALUE && ::GetFileType(h) == FILE_TYPE_CHAR;
+  }
 #endif
 }
 


        
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to