diff --git a/compat/win/sys/types.h b/compat/win/sys/types.h index 31797321..5355bcf7 100644 --- a/compat/win/sys/types.h +++ b/compat/win/sys/types.h @@ -24,12 +24,6 @@ #include -#ifdef _WIN64 -typedef int64_t ssize_t; -#else -typedef __w64 int ssize_t; -#endif - typedef unsigned int pid_t; #endif // CRASHPAD_COMPAT_WIN_SYS_TYPES_H_ diff --git a/tools/crashpad_database_util.cc b/tools/crashpad_database_util.cc index e09f1ded..433b4769 100644 --- a/tools/crashpad_database_util.cc +++ b/tools/crashpad_database_util.cc @@ -566,7 +566,7 @@ int DatabaseUtilMain(int argc, char* argv[]) { call_error_writing_crash_report(database.get(), new_report); char buf[4096]; - ssize_t read_result; + FileOperationResult read_result; do { read_result = file_reader->Read(buf, sizeof(buf)); if (read_result < 0) { diff --git a/util/file/file_io.cc b/util/file/file_io.cc index 6f2d0b58..98eb3404 100644 --- a/util/file/file_io.cc +++ b/util/file/file_io.cc @@ -20,8 +20,8 @@ namespace crashpad { bool LoggingReadFile(FileHandle file, void* buffer, size_t size) { - ssize_t expect = base::checked_cast(size); - ssize_t rv = ReadFile(file, buffer, size); + FileOperationResult expect = base::checked_cast(size); + FileOperationResult rv = ReadFile(file, buffer, size); if (rv < 0) { PLOG(ERROR) << "read"; return false; @@ -35,8 +35,8 @@ bool LoggingReadFile(FileHandle file, void* buffer, size_t size) { } bool LoggingWriteFile(FileHandle file, const void* buffer, size_t size) { - ssize_t expect = base::checked_cast(size); - ssize_t rv = WriteFile(file, buffer, size); + FileOperationResult expect = base::checked_cast(size); + FileOperationResult rv = WriteFile(file, buffer, size); if (rv < 0) { PLOG(ERROR) << "write"; return false; @@ -59,7 +59,7 @@ void CheckedWriteFile(FileHandle file, const void* buffer, size_t size) { void CheckedReadFileAtEOF(FileHandle file) { char c; - ssize_t rv = ReadFile(file, &c, 1); + FileOperationResult rv = ReadFile(file, &c, 1); if (rv < 0) { PCHECK(rv == 0) << "read"; } else { diff --git a/util/file/file_reader.cc b/util/file/file_reader.cc index aad55cf7..14d06c74 100644 --- a/util/file/file_reader.cc +++ b/util/file/file_reader.cc @@ -21,8 +21,8 @@ namespace crashpad { bool FileReaderInterface::ReadExactly(void* data, size_t size) { - ssize_t expect = base::checked_cast(size); - ssize_t rv = Read(data, size); + FileOperationResult expect = base::checked_cast(size); + FileOperationResult rv = Read(data, size); if (rv < 0) { // Read() will have logged its own error. return false; @@ -41,14 +41,14 @@ WeakFileHandleFileReader::WeakFileHandleFileReader(FileHandle file_handle) WeakFileHandleFileReader::~WeakFileHandleFileReader() { } -ssize_t WeakFileHandleFileReader::Read(void* data, size_t size) { +FileOperationResult WeakFileHandleFileReader::Read(void* data, size_t size) { DCHECK_NE(file_handle_, kInvalidFileHandle); // Don’t use LoggingReadFile(), which insists on a full read and only returns // a bool. This method permits short reads and returns the number of bytes // read. - base::checked_cast(size); - ssize_t rv = ReadFile(file_handle_, data, size); + base::checked_cast(size); + FileOperationResult rv = ReadFile(file_handle_, data, size); if (rv < 0) { PLOG(ERROR) << "read"; return -1; @@ -88,7 +88,7 @@ void FileReader::Close() { file_.reset(); } -ssize_t FileReader::Read(void* data, size_t size) { +FileOperationResult FileReader::Read(void* data, size_t size) { DCHECK(file_.is_valid()); return weak_file_handle_file_reader_.Read(data, size); } @@ -105,7 +105,7 @@ WeakStdioFileReader::WeakStdioFileReader(FILE* file) WeakStdioFileReader::~WeakStdioFileReader() { } -ssize_t WeakStdioFileReader::Read(void* data, size_t size) { +FileOperationResult WeakStdioFileReader::Read(void* data, size_t size) { DCHECK(file_); size_t rv = fread(data, 1, size, file_); diff --git a/util/file/file_reader.h b/util/file/file_reader.h index 58f58651..5d77b875 100644 --- a/util/file/file_reader.h +++ b/util/file/file_reader.h @@ -37,7 +37,7 @@ class FileReaderInterface : public virtual FileSeekerInterface { //! \return The number of bytes actually read if the operation succeeded, //! which may be `0` or any positive value less than or equal to \a size. //! `-1` if the operation failed, with an error message logged. - virtual ssize_t Read(void* data, size_t size) = 0; + virtual FileOperationResult Read(void* data, size_t size) = 0; //! \brief Wraps Read(), ensuring that the read succeeded and exactly \a size //! bytes were read. @@ -69,7 +69,7 @@ class WeakFileHandleFileReader : public FileReaderInterface { ~WeakFileHandleFileReader() override; // FileReaderInterface: - ssize_t Read(void* data, size_t size) override; + FileOperationResult Read(void* data, size_t size) override; // FileSeekerInterface: @@ -125,7 +125,7 @@ class FileReader : public FileReaderInterface { //! //! \note It is only valid to call this method between a successful Open() and //! a Close(). - ssize_t Read(void* data, size_t size) override; + FileOperationResult Read(void* data, size_t size) override; // FileSeekerInterface: @@ -160,7 +160,7 @@ class WeakStdioFileReader : public FileReaderInterface { ~WeakStdioFileReader() override; // FileReaderInterface: - ssize_t Read(void* data, size_t size) override; + FileOperationResult Read(void* data, size_t size) override; // FileSeekerInterface: diff --git a/util/file/string_file.cc b/util/file/string_file.cc index 061b275b..1c74b3f0 100644 --- a/util/file/string_file.cc +++ b/util/file/string_file.cc @@ -33,8 +33,9 @@ StringFile::~StringFile() { } void StringFile::SetString(const std::string& string) { - CHECK_LE(string.size(), - implicit_cast(std::numeric_limits::max())); + CHECK_LE( + string.size(), + implicit_cast(std::numeric_limits::max())); string_ = string; offset_ = 0; } @@ -44,7 +45,7 @@ void StringFile::Reset() { offset_ = 0; } -ssize_t StringFile::Read(void* data, size_t size) { +FileOperationResult StringFile::Read(void* data, size_t size) { DCHECK(offset_.IsValid()); const size_t offset = offset_.ValueOrDie(); @@ -54,7 +55,7 @@ ssize_t StringFile::Read(void* data, size_t size) { const size_t nread = std::min(size, string_.size() - offset); - base::CheckedNumeric new_offset = offset_; + base::CheckedNumeric new_offset = offset_; new_offset += nread; if (!new_offset.IsValid()) { LOG(ERROR) << "Read(): file too large"; @@ -75,7 +76,7 @@ bool StringFile::Write(const void* data, size_t size) { string_.resize(offset); } - base::CheckedNumeric new_offset = offset_; + base::CheckedNumeric new_offset = offset_; new_offset += size; if (!new_offset.IsValid()) { LOG(ERROR) << "Write(): file too large"; @@ -97,7 +98,7 @@ bool StringFile::WriteIoVec(std::vector* iovecs) { } // Avoid writing anything at all if it would cause an overflow. - base::CheckedNumeric new_offset = offset_; + base::CheckedNumeric new_offset = offset_; for (const WritableIoVec& iov : *iovecs) { new_offset += iov.iov_len; if (!new_offset.IsValid()) { diff --git a/util/file/string_file.h b/util/file/string_file.h index c2e18ada..05f57072 100644 --- a/util/file/string_file.h +++ b/util/file/string_file.h @@ -21,6 +21,7 @@ #include "base/basictypes.h" #include "base/numerics/safe_math.h" +#include "util/file/file_io.h" #include "util/file/file_reader.h" #include "util/file/file_writer.h" @@ -50,7 +51,7 @@ class StringFile : public FileReaderInterface, public FileWriterInterface { void Reset(); // FileReaderInterface: - ssize_t Read(void* data, size_t size) override; + FileOperationResult Read(void* data, size_t size) override; // FileWriterInterface: bool Write(const void* data, size_t size) override; diff --git a/util/file/string_file_test.cc b/util/file/string_file_test.cc index e2a8214b..0990daf0 100644 --- a/util/file/string_file_test.cc +++ b/util/file/string_file_test.cc @@ -197,13 +197,16 @@ TEST(StringFile, WriteInvalid) { EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); EXPECT_FALSE(string_file.Write( - "", implicit_cast(std::numeric_limits::max()) + 1)); + "", + implicit_cast(std::numeric_limits::max()) + + 1)); EXPECT_TRUE(string_file.string().empty()); EXPECT_EQ(0, string_file.Seek(0, SEEK_CUR)); EXPECT_TRUE(string_file.Write("a", 1)); EXPECT_FALSE(string_file.Write( - "", implicit_cast(std::numeric_limits::max()))); + "", + implicit_cast(std::numeric_limits::max()))); EXPECT_EQ(1u, string_file.string().size()); EXPECT_EQ("a", string_file.string()); EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); @@ -290,7 +293,7 @@ TEST(StringFile, WriteIoVecInvalid) { WritableIoVec iov; EXPECT_EQ(1, string_file.Seek(1, SEEK_CUR)); iov.iov_base = "a"; - iov.iov_len = std::numeric_limits::max(); + iov.iov_len = std::numeric_limits::max(); iovecs.push_back(iov); EXPECT_FALSE(string_file.WriteIoVec(&iovecs)); EXPECT_TRUE(string_file.string().empty()); @@ -300,7 +303,7 @@ TEST(StringFile, WriteIoVecInvalid) { iov.iov_base = "a"; iov.iov_len = 1; iovecs.push_back(iov); - iov.iov_len = std::numeric_limits::max() - 1; + iov.iov_len = std::numeric_limits::max() - 1; iovecs.push_back(iov); EXPECT_FALSE(string_file.WriteIoVec(&iovecs)); EXPECT_TRUE(string_file.string().empty()); @@ -449,7 +452,9 @@ TEST(StringFile, SeekInvalid) { EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); EXPECT_LT(string_file.Seek(-1, SEEK_SET), 0); EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); - EXPECT_LT(string_file.Seek(std::numeric_limits::min(), SEEK_SET), 0); + EXPECT_LT(string_file.Seek(std::numeric_limits::min(), + SEEK_SET), + 0); EXPECT_EQ(1, string_file.Seek(0, SEEK_CUR)); EXPECT_LT(string_file.Seek(std::numeric_limits::min(), SEEK_SET), 0); @@ -487,7 +492,8 @@ TEST(StringFile, SeekSet) { EXPECT_EQ(10, string_file.Seek(0, SEEK_CUR)); EXPECT_FALSE(string_file.SeekSet(-1)); EXPECT_EQ(10, string_file.Seek(0, SEEK_CUR)); - EXPECT_FALSE(string_file.SeekSet(std::numeric_limits::min())); + EXPECT_FALSE( + string_file.SeekSet(std::numeric_limits::min())); EXPECT_EQ(10, string_file.Seek(0, SEEK_CUR)); EXPECT_FALSE(string_file.SeekSet(std::numeric_limits::min())); EXPECT_EQ(10, string_file.Seek(0, SEEK_CUR)); diff --git a/util/net/http_body.cc b/util/net/http_body.cc index 00acead8..6bc6ec9e 100644 --- a/util/net/http_body.cc +++ b/util/net/http_body.cc @@ -32,15 +32,16 @@ StringHTTPBodyStream::StringHTTPBodyStream(const std::string& string) StringHTTPBodyStream::~StringHTTPBodyStream() { } -ssize_t StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { +FileOperationResult StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, + size_t max_len) { size_t num_bytes_remaining = string_.length() - bytes_read_; if (num_bytes_remaining == 0) { return num_bytes_remaining; } - size_t num_bytes_returned = - std::min(std::min(num_bytes_remaining, max_len), - implicit_cast(std::numeric_limits::max())); + size_t num_bytes_returned = std::min( + std::min(num_bytes_remaining, max_len), + implicit_cast(std::numeric_limits::max())); memcpy(buffer, &string_[bytes_read_], num_bytes_returned); bytes_read_ += num_bytes_returned; return num_bytes_returned; @@ -53,7 +54,8 @@ FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path) FileHTTPBodyStream::~FileHTTPBodyStream() { } -ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { +FileOperationResult FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, + size_t max_len) { switch (file_state_) { case kUnopenedFile: file_.reset(LoggingOpenFileForRead(path_)); @@ -71,7 +73,7 @@ ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { break; } - ssize_t rv = ReadFile(file_.get(), buffer, max_len); + FileOperationResult rv = ReadFile(file_.get(), buffer, max_len); if (rv == 0) { file_.reset(); file_state_ = kClosedAtEOF; @@ -90,14 +92,16 @@ CompositeHTTPBodyStream::~CompositeHTTPBodyStream() { STLDeleteContainerPointers(parts_.begin(), parts_.end()); } -ssize_t CompositeHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, - size_t buffer_len) { - ssize_t max_len = std::min( - buffer_len, implicit_cast(std::numeric_limits::max())); - ssize_t bytes_copied = 0; +FileOperationResult CompositeHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, + size_t buffer_len) { + FileOperationResult max_len = std::min( + buffer_len, + implicit_cast(std::numeric_limits::max())); + FileOperationResult bytes_copied = 0; while (bytes_copied < max_len && current_part_ != parts_.end()) { - ssize_t this_read = (*current_part_)->GetBytesBuffer( - buffer + bytes_copied, max_len - bytes_copied); + FileOperationResult this_read = + (*current_part_) + ->GetBytesBuffer(buffer + bytes_copied, max_len - bytes_copied); if (this_read == 0) { // If the current part has returned 0 indicating EOF, advance the current diff --git a/util/net/http_body.h b/util/net/http_body.h index 3cc2f19d..1646591e 100644 --- a/util/net/http_body.h +++ b/util/net/http_body.h @@ -42,7 +42,8 @@ class HTTPBodyStream { //! \return On success, a positive number indicating the number of bytes //! actually copied to \a buffer. On failure, a negative number. When //! the stream has no more data, returns `0`. - virtual ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) = 0; + virtual FileOperationResult GetBytesBuffer(uint8_t* buffer, + size_t max_len) = 0; protected: HTTPBodyStream() {} @@ -60,7 +61,7 @@ class StringHTTPBodyStream : public HTTPBodyStream { ~StringHTTPBodyStream() override; // HTTPBodyStream: - ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override; + FileOperationResult GetBytesBuffer(uint8_t* buffer, size_t max_len) override; private: std::string string_; @@ -81,7 +82,7 @@ class FileHTTPBodyStream : public HTTPBodyStream { ~FileHTTPBodyStream() override; // HTTPBodyStream: - ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override; + FileOperationResult GetBytesBuffer(uint8_t* buffer, size_t max_len) override; private: enum FileState { @@ -115,7 +116,7 @@ class CompositeHTTPBodyStream : public HTTPBodyStream { ~CompositeHTTPBodyStream() override; // HTTPBodyStream: - ssize_t GetBytesBuffer(uint8_t* buffer, size_t max_len) override; + FileOperationResult GetBytesBuffer(uint8_t* buffer, size_t max_len) override; private: PartsList parts_; diff --git a/util/net/http_body_test.cc b/util/net/http_body_test.cc index 8ec334d3..ea4d8693 100644 --- a/util/net/http_body_test.cc +++ b/util/net/http_body_test.cc @@ -47,7 +47,7 @@ TEST(StringHTTPBodyStream, SmallString) { std::string string("Hello, world"); StringHTTPBodyStream stream(string); - EXPECT_EQ(implicit_cast(string.length()), + EXPECT_EQ(implicit_cast(string.length()), stream.GetBytesBuffer(buf, sizeof(buf))); std::string actual(reinterpret_cast(buf), string.length()); diff --git a/util/net/http_body_test_util.cc b/util/net/http_body_test_util.cc index 3d732311..c3dbd19c 100644 --- a/util/net/http_body_test_util.cc +++ b/util/net/http_body_test_util.cc @@ -18,6 +18,7 @@ #include "base/memory/scoped_ptr.h" #include "gtest/gtest.h" +#include "util/file/file_io.h" #include "util/net/http_body.h" namespace crashpad { @@ -31,7 +32,7 @@ std::string ReadStreamToString(HTTPBodyStream* stream, size_t buffer_size) { scoped_ptr buf(new uint8_t[buffer_size]); std::string result; - ssize_t bytes_read; + FileOperationResult bytes_read; while ((bytes_read = stream->GetBytesBuffer(buf.get(), buffer_size)) != 0) { if (bytes_read < 0) { ADD_FAILURE() << "Failed to read from stream: " << bytes_read; diff --git a/util/net/http_transport_mac.mm b/util/net/http_transport_mac.mm index 971a9986..aad505d5 100644 --- a/util/net/http_transport_mac.mm +++ b/util/net/http_transport_mac.mm @@ -22,6 +22,7 @@ #include "base/strings/stringprintf.h" #include "base/strings/sys_string_conversions.h" #include "third_party/apple_cf/CFStreamAbstract.h" +#include "util/file/file_io.h" #include "util/misc/implicit_cast.h" #include "util/net/http_body.h" @@ -93,7 +94,8 @@ class HTTPBodyStreamCFReadStream { return 0; } - ssize_t bytes_read = GetStream(info)->GetBytesBuffer(buffer, buffer_length); + FileOperationResult bytes_read = + GetStream(info)->GetBytesBuffer(buffer, buffer_length); if (bytes_read < 0) { error->error = -1; error->domain = kCFStreamErrorDomainCustom; diff --git a/util/net/http_transport_test.cc b/util/net/http_transport_test.cc index 0bb29857..fe409998 100644 --- a/util/net/http_transport_test.cc +++ b/util/net/http_transport_test.cc @@ -121,7 +121,7 @@ class HTTPTransportTestFixture : public MultiprocessExec { // Read until the child's stdout closes. std::string request; char buf[32]; - ssize_t bytes_read; + FileOperationResult bytes_read; while ((bytes_read = ReadFile(ReadPipeHandle(), buf, sizeof(buf))) != 0) { ASSERT_GE(bytes_read, 0); request.append(buf, bytes_read); diff --git a/util/net/http_transport_win.cc b/util/net/http_transport_win.cc index e053311d..9c3d00fe 100644 --- a/util/net/http_transport_win.cc +++ b/util/net/http_transport_win.cc @@ -22,8 +22,9 @@ #include "base/scoped_generic.h" #include "base/strings/stringprintf.h" #include "base/strings/utf_string_conversions.h" -#include "util/net/http_body.h" #include "package.h" +#include "util/file/file_io.h" +#include "util/net/http_body.h" namespace crashpad { @@ -174,7 +175,7 @@ bool HTTPTransportWin::ExecuteSynchronously(std::string* response_body) { const size_t kBufferSize = 4096; for (;;) { uint8_t buffer[kBufferSize]; - ssize_t bytes_to_write = + FileOperationResult bytes_to_write = body_stream()->GetBytesBuffer(buffer, sizeof(buffer)); if (bytes_to_write == 0) break;