]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix filebuf::native_handle() for Windows
authorJonathan Wakely <jwakely@redhat.com>
Thu, 14 Dec 2023 23:22:28 +0000 (23:22 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 14 Dec 2023 23:59:21 +0000 (23:59 +0000)
libstdc++-v3/ChangeLog:

* acinclude.m4 (GLIBCXX_CHECK_FILEBUF_NATIVE_HANDLES): Add
missing header to configure test. Check correct variable.
* config/io/basic_file_stdio.cc (__basic_file<char>::native_handle):
Fix typo.
* configure: Regenerate.
* testsuite/27_io/basic_filebuf/native_handle/char/1.cc: Do not
call CloseHandle on the native handle.
* testsuite/27_io/basic_filebuf/native_handle/wchar_t/1.cc:
Likewise.

libstdc++-v3/acinclude.m4
libstdc++-v3/config/io/basic_file_stdio.cc
libstdc++-v3/configure
libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/char/1.cc
libstdc++-v3/testsuite/27_io/basic_filebuf/native_handle/wchar_t/1.cc

index 77569d5bb59448358c0fe28546e7ffed165284f5..e7cbf0fcf960335ccec8a5928b590d0dd8762dc7 100644 (file)
@@ -5803,6 +5803,7 @@ AC_LANG_SAVE
   AC_TRY_COMPILE([
   #if defined(_WIN32) && !defined(__CYGWIN__)
   # include <stdint.h>
+  # include <stdio.h>
   # include <io.h>
   #endif
   ],[
@@ -5811,7 +5812,7 @@ AC_LANG_SAVE
     intptr_t crt_handle = _get_osfhandle(fd);
     void* win32_handle = reinterpret_cast<void*>(crt_handle);
   ], [ac_get_osfhandle=yes], [ac_get_osfhandle=no])
-  if test "$ac_objext" = yes; then
+  if test "$ac_get_osfhandle" = yes; then
     AC_DEFINE_UNQUOTED(_GLIBCXX_USE__GET_OSFHANDLE, 1,
       [Define if _get_osfhandle should be used for filebuf::native_handle().])
   fi
index a33b53b3b271c279bcd05f790e1dc4fe15ef9af6..f85b6e9be0854f0a97aca7a0de109c83360d48bc 100644 (file)
@@ -472,7 +472,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     return _M_cfile;
 #elif _GLIBCXX_USE__GET_OSFHANDLE
     const intptr_t handle = _M_cfile ? _get_osfhandle(fileno(_M_cfile)) : -1;
-    return reinterpret_cast<native_handle>(handle);
+    return reinterpret_cast<native_handle_type>(handle);
 #else
     return fileno(_M_cfile);
 #endif
index 5bed1fb81101b1d1370046a426a631cefc15ff3e..bef2b840a595b6ec3a2c24eb3efb04f6835f62b7 100755 (executable)
@@ -54476,6 +54476,7 @@ $as_echo_n "checking whether _get_osfhandle is defined in <io.h>... " >&6; }
 
   #if defined(_WIN32) && !defined(__CYGWIN__)
   # include <stdint.h>
+  # include <stdio.h>
   # include <io.h>
   #endif
 
@@ -54498,7 +54499,7 @@ else
   ac_get_osfhandle=no
 fi
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-  if test "$ac_objext" = yes; then
+  if test "$ac_get_osfhandle" = yes; then
 
 cat >>confdefs.h <<_ACEOF
 #define _GLIBCXX_USE__GET_OSFHANDLE 1
index 0551f260ade5e6ac75a7955d49631aab8940f646..3ab297ed6ebf721019a66ebf64e4b78d9d3f8d3a 100644 (file)
@@ -17,8 +17,9 @@ using type = std::basic_filebuf<char>::native_handle_type;
 #if __has_include(<unistd.h>)
 # include <unistd.h> // close(int)
 #endif
-#if __has_include(<handleapi.h>)
-# include <handleapi.h> // CloseHandle(HANDLE)
+#if __has_include(<io.h>)
+# include <io.h> // _open_osfhandle
+# include <fcntl.h> // _O_RDONLY, _O_TEXT
 #endif
 
 #include <testsuite_hooks.h>
@@ -38,8 +39,8 @@ test01()
       ::close(handle); // POSIX
 #endif
 #if __has_include(<handleapi.h>)
-    else if constexpr (std::is_same_v<HandleT, void*>)
-      ::CloseHandle(handle); // Windows
+    else if constexpr (std::is_same_v<HandleT, void*>) // Windows
+      ::_close(::_open_osfhandle((intptr_t)handle, _O_RDONLY|_O_TEXT));
 #endif
     else
       VERIFY( false );
index 7ee5e4e8f538c564c833ed032b2ebe6d7c1b5e48..5917c3d29c635c33417adb59d05160c65a74d50b 100644 (file)
@@ -10,8 +10,9 @@ using type = std::basic_filebuf<wchar_t>::native_handle_type;
 #if __has_include(<unistd.h>)
 # include <unistd.h> // close(int)
 #endif
-#if __has_include(<handleapi.h>)
-# include <handleapi.h> // CloseHandle(HANDLE)
+#if __has_include(<io.h>)
+# include <io.h> // _open_osfhandle
+# include <fcntl.h> // _O_RDONLY, _O_TEXT
 #endif
 
 #include <testsuite_hooks.h>
@@ -31,8 +32,8 @@ test01()
       ::close(handle); // POSIX
 #endif
 #if __has_include(<handleapi.h>)
-    else if constexpr (std::is_same_v<HandleT, void*>)
-      ::CloseHandle(handle); // Windows
+    else if constexpr (std::is_same_v<HandleT, void*>) // Windows
+      ::_close(::_open_osfhandle((intptr_t)handle, _O_RDONLY|_O_TEXT));
 #endif
     else
       VERIFY( false );