diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index 68f67565..5766cb14 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -102,9 +102,19 @@ 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, count))); + FMT_RETRY(result, ::FMT_POSIX(read(fd_, buffer, ConvertRWCount(count)))); if (result == -1) fmt::ThrowSystemError(errno, "cannot read from file"); return result;