diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 077bd476..9215fa7c 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -504,28 +504,6 @@ TEST(FileTest, Read) { EXPECT_READ(f, "language: cpp"); } -#ifdef _WIN32 -// The count argument to the read function is unsigned on Windows, so check -// if integer overflow is handled properly. -TEST(FileTest, ReadCountOverflow) { - if (sizeof(std::size_t) == sizeof(unsigned)) - return; - File read_end, write_end; - File::pipe(read_end, write_end); - Write(write_end, "test"); - write_end.close(); - char buffer[4]; - std::size_t offset = 0; - std::size_t count = std::numeric_limits::max() + std::size_t(1); - std::streamsize n = 0; - do { - n = read_end.read(buffer + offset, count); - offset += n; - } while (n != 0); - EXPECT_EQ("test", std::string(buffer, offset)); -} -#endif - TEST(FileTest, ReadError) { File read_end, write_end; File::pipe(read_end, write_end); diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index 5766cb14..ec08f87a 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -30,6 +30,7 @@ #if FMT_USE_FILE_DESCRIPTORS #include +#include #include #include @@ -51,6 +52,19 @@ result = (expression); \ } while (result == -1 && errno == EINTR) +namespace { +// TODO: test +#ifdef _WIN32 +// On Windows the count argument to read and write is unsigned, so convert +// it from size_t preventing integer overflow. +inline unsigned ConvertRWCount(std::size_t count) { + return count <= UINT_MAX ? count : UINT_MAX; +} +#else +inline std::size_t ConvertRWCount(std::size_t count) { return count; } +#endif +} + BufferedFile::~BufferedFile() FMT_NOEXCEPT(true) { if (file_ && std::fclose(file_) != 0) fmt::ReportSystemError(errno, "cannot close file"); @@ -102,16 +116,6 @@ void File::close() { fmt::ThrowSystemError(errno, "cannot close file"); } -#ifdef _WIN32 -// On Windows the count argument to read and write is unsigned, so convert -// it from size_t preventing integer overflow. -inline unsigned ConvertRWCount(std::size_t count) { - return count <= UINT_MAX ? count : UINT_MAX; -} -#else -inline std::size_t ConvertRWCount(std::size_t count) { return count; } -#endif - std::streamsize File::read(void *buffer, std::size_t count) { std::streamsize result = 0; FMT_RETRY(result, ::FMT_POSIX(read(fd_, buffer, ConvertRWCount(count)))); @@ -122,7 +126,7 @@ std::streamsize File::read(void *buffer, std::size_t count) { std::streamsize File::write(const void *buffer, std::size_t count) { std::streamsize result = 0; - FMT_RETRY(result, ::FMT_POSIX(write(fd_, buffer, count))); + FMT_RETRY(result, ::FMT_POSIX(write(fd_, buffer, ConvertRWCount(count)))); if (result == -1) fmt::ThrowSystemError(errno, "cannot write to file"); return result;