Commit aab84f67 authored by Victor Zverovich's avatar Victor Zverovich

Fix warnings.

parent 6bd3880c
...@@ -504,28 +504,6 @@ TEST(FileTest, Read) { ...@@ -504,28 +504,6 @@ TEST(FileTest, Read) {
EXPECT_READ(f, "language: cpp"); 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<unsigned>::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) { TEST(FileTest, ReadError) {
File read_end, write_end; File read_end, write_end;
File::pipe(read_end, write_end); File::pipe(read_end, write_end);
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#if FMT_USE_FILE_DESCRIPTORS #if FMT_USE_FILE_DESCRIPTORS
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -51,6 +52,19 @@ ...@@ -51,6 +52,19 @@
result = (expression); \ result = (expression); \
} while (result == -1 && errno == EINTR) } 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) { BufferedFile::~BufferedFile() FMT_NOEXCEPT(true) {
if (file_ && std::fclose(file_) != 0) if (file_ && std::fclose(file_) != 0)
fmt::ReportSystemError(errno, "cannot close file"); fmt::ReportSystemError(errno, "cannot close file");
...@@ -102,16 +116,6 @@ void File::close() { ...@@ -102,16 +116,6 @@ void File::close() {
fmt::ThrowSystemError(errno, "cannot close file"); 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 File::read(void *buffer, std::size_t count) {
std::streamsize result = 0; std::streamsize result = 0;
FMT_RETRY(result, ::FMT_POSIX(read(fd_, buffer, ConvertRWCount(count)))); FMT_RETRY(result, ::FMT_POSIX(read(fd_, buffer, ConvertRWCount(count))));
...@@ -122,7 +126,7 @@ std::streamsize File::read(void *buffer, std::size_t 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 File::write(const void *buffer, std::size_t count) {
std::streamsize result = 0; 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) if (result == -1)
fmt::ThrowSystemError(errno, "cannot write to file"); fmt::ThrowSystemError(errno, "cannot write to file");
return result; return result;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment