Fix warnings.

This commit is contained in:
Victor Zverovich 2014-05-06 11:24:20 -07:00
parent 6bd3880cf1
commit aab84f67a4
2 changed files with 15 additions and 33 deletions

View File

@ -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<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) {
File read_end, write_end;
File::pipe(read_end, write_end);

View File

@ -30,6 +30,7 @@
#if FMT_USE_FILE_DESCRIPTORS
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
@ -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;