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

>From 8d40314a303c9057137a9e614d0cf9a4cdf2b366 Mon Sep 17 00:00:00 2001
From: Charles Zablit <[email protected]>
Date: Tue, 5 May 2026 16:14:08 +0100
Subject: [PATCH] [lldb-dap][windows] fix crash

---
 lldb/source/Host/common/File.cpp | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

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