From bfff9fc387b1fabf573a5d1b926e946958f5ff58 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 24 Aug 2026 13:24:48 -0700 Subject: [PATCH] Use non-POSIX function names on MSVC to avoid errors when _CRT_DECLARE_NONSTDC_NAMES = 0 Fixes: #4571 PiperOrigin-RevId: 970040273 Change-Id: Id1aed06aeddde7dbcdff6eaa4702f356c034cadb --- .../include/gtest/internal/gtest-port.h | 48 +++++++++++++++++-- googletest/src/gtest-port.cc | 12 ++--- googletest/test/gtest_unittest.cc | 6 ++- 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index f13ea49ee..58c287989 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -2188,7 +2188,7 @@ inline int IsATTY(int fd) { GTEST_DISABLE_DEPRECATED_PUSH_() -// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and +// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), Dup(), Dup2(), and // StrError() aren't needed on Windows CE at this time and thus not // defined there. #if GTEST_HAS_FILE_SYSTEM @@ -2196,7 +2196,13 @@ GTEST_DISABLE_DEPRECATED_PUSH_() !defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) && \ !defined(GTEST_OS_ESP8266) && !defined(GTEST_OS_XTENSA) && \ !defined(GTEST_OS_QURT) -inline int ChDir(const char* dir) { return chdir(dir); } +inline int ChDir(const char* dir) { +#ifdef GTEST_OS_WINDOWS + return _chdir(dir); +#else + return chdir(dir); +#endif +} #endif inline FILE* FOpen(const char* path, const char* mode) { #if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW) @@ -2213,17 +2219,51 @@ inline FILE* FOpen(const char* path, const char* mode) { inline FILE* FReopen(const char* path, const char* mode, FILE* stream) { return freopen(path, mode, stream); } -inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); } +inline FILE* FDOpen(int fd, const char* mode) { +#ifdef GTEST_OS_WINDOWS + return _fdopen(fd, mode); +#else + return fdopen(fd, mode); +#endif +} #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT inline int FClose(FILE* fp) { return fclose(fp); } #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT) inline int Read(int fd, void* buf, unsigned int count) { +#ifdef GTEST_OS_WINDOWS + return static_cast(_read(fd, buf, count)); +#else return static_cast(read(fd, buf, count)); +#endif } inline int Write(int fd, const void* buf, unsigned int count) { +#ifdef GTEST_OS_WINDOWS + return static_cast(_write(fd, buf, count)); +#else return static_cast(write(fd, buf, count)); +#endif +} +inline int Close(int fd) { +#ifdef GTEST_OS_WINDOWS + return _close(fd); +#else + return close(fd); +#endif +} +inline int Dup(int fd) { +#ifdef GTEST_OS_WINDOWS + return _dup(fd); +#else + return dup(fd); +#endif +} +inline int Dup2(int fd1, int fd2) { +#ifdef GTEST_OS_WINDOWS + return _dup2(fd1, fd2); +#else + return dup2(fd1, fd2); +#endif } -inline int Close(int fd) { return close(fd); } #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT #endif // GTEST_HAS_FILE_SYSTEM diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc index 433586f3b..0023a887a 100644 --- a/googletest/src/gtest-port.cc +++ b/googletest/src/gtest-port.cc @@ -1085,7 +1085,7 @@ bool EndsWithPathSeparator(const std::string& path) { class CapturedStream { public: // The ctor redirects the stream to a temporary file. - explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) { + explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(posix::Dup(fd)) { #ifdef GTEST_OS_WINDOWS char temp_dir_path[MAX_PATH + 1] = {'\0'}; // NOLINT char temp_file_path[MAX_PATH + 1] = {'\0'}; // NOLINT @@ -1097,7 +1097,7 @@ class CapturedStream { GTEST_CHECK_(success != 0) << "Failed to create temporary file in " << temp_dir_path << " with error " << ::GetLastError(); - const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE); + const int captured_fd = _creat(temp_file_path, _S_IREAD | _S_IWRITE); GTEST_CHECK_(captured_fd != -1) << "Failed to open temporary file " << temp_file_path << " with error " << ::GetLastError(); @@ -1167,8 +1167,8 @@ class CapturedStream { filename_ = std::move(name_template); #endif // GTEST_OS_WINDOWS fflush(nullptr); - dup2(captured_fd, fd_); - close(captured_fd); + posix::Dup2(captured_fd, fd_); + posix::Close(captured_fd); } ~CapturedStream() { remove(filename_.c_str()); } @@ -1177,8 +1177,8 @@ class CapturedStream { if (uncaptured_fd_ != -1) { // Restores the original stream. fflush(nullptr); - dup2(uncaptured_fd_, fd_); - close(uncaptured_fd_); + posix::Dup2(uncaptured_fd_, fd_); + posix::Close(uncaptured_fd_); uncaptured_fd_ = -1; } diff --git a/googletest/test/gtest_unittest.cc b/googletest/test/gtest_unittest.cc index a957c895d..f4d718970 100644 --- a/googletest/test/gtest_unittest.cc +++ b/googletest/test/gtest_unittest.cc @@ -484,9 +484,11 @@ class FormatEpochTimeInMillisAsIso8601Test : public Test { const std::string env_var = std::string("TZ=") + (time_zone ? time_zone : ""); _putenv(env_var.c_str()); - GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996 /* deprecated function */) +#if defined(GTEST_OS_WINDOWS) + _tzset(); +#else tzset(); - GTEST_DISABLE_MSC_WARNINGS_POP_() +#endif #else #if defined(GTEST_OS_LINUX_ANDROID) && __ANDROID_API__ < 21 // Work around KitKat bug in tzset by setting "UTC" before setting "UTC+00".