Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: util/file/string_file.cc

Issue 1416493006: Change file op |ssize_t|s to FileOperationResult (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: fixes Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/file/string_file.h ('k') | util/file/string_file_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
(...skipping 15 matching lines...) Expand all
26 26
27 namespace crashpad { 27 namespace crashpad {
28 28
29 StringFile::StringFile() : string_(), offset_(0) { 29 StringFile::StringFile() : string_(), offset_(0) {
30 } 30 }
31 31
32 StringFile::~StringFile() { 32 StringFile::~StringFile() {
33 } 33 }
34 34
35 void StringFile::SetString(const std::string& string) { 35 void StringFile::SetString(const std::string& string) {
36 CHECK_LE(string.size(), 36 CHECK_LE(
37 implicit_cast<size_t>(std::numeric_limits<ssize_t>::max())); 37 string.size(),
38 implicit_cast<size_t>(std::numeric_limits<FileOperationResult>::max()));
38 string_ = string; 39 string_ = string;
39 offset_ = 0; 40 offset_ = 0;
40 } 41 }
41 42
42 void StringFile::Reset() { 43 void StringFile::Reset() {
43 string_.clear(); 44 string_.clear();
44 offset_ = 0; 45 offset_ = 0;
45 } 46 }
46 47
47 ssize_t StringFile::Read(void* data, size_t size) { 48 FileOperationResult StringFile::Read(void* data, size_t size) {
48 DCHECK(offset_.IsValid()); 49 DCHECK(offset_.IsValid());
49 50
50 const size_t offset = offset_.ValueOrDie(); 51 const size_t offset = offset_.ValueOrDie();
51 if (offset >= string_.size()) { 52 if (offset >= string_.size()) {
52 return 0; 53 return 0;
53 } 54 }
54 55
55 const size_t nread = std::min(size, string_.size() - offset); 56 const size_t nread = std::min(size, string_.size() - offset);
56 57
57 base::CheckedNumeric<ssize_t> new_offset = offset_; 58 base::CheckedNumeric<FileOperationResult> new_offset = offset_;
58 new_offset += nread; 59 new_offset += nread;
59 if (!new_offset.IsValid()) { 60 if (!new_offset.IsValid()) {
60 LOG(ERROR) << "Read(): file too large"; 61 LOG(ERROR) << "Read(): file too large";
61 return -1; 62 return -1;
62 } 63 }
63 64
64 memcpy(data, &string_[offset], nread); 65 memcpy(data, &string_[offset], nread);
65 offset_ = new_offset; 66 offset_ = new_offset;
66 67
67 return nread; 68 return nread;
68 } 69 }
69 70
70 bool StringFile::Write(const void* data, size_t size) { 71 bool StringFile::Write(const void* data, size_t size) {
71 DCHECK(offset_.IsValid()); 72 DCHECK(offset_.IsValid());
72 73
73 const size_t offset = offset_.ValueOrDie(); 74 const size_t offset = offset_.ValueOrDie();
74 if (offset > string_.size()) { 75 if (offset > string_.size()) {
75 string_.resize(offset); 76 string_.resize(offset);
76 } 77 }
77 78
78 base::CheckedNumeric<ssize_t> new_offset = offset_; 79 base::CheckedNumeric<FileOperationResult> new_offset = offset_;
79 new_offset += size; 80 new_offset += size;
80 if (!new_offset.IsValid()) { 81 if (!new_offset.IsValid()) {
81 LOG(ERROR) << "Write(): file too large"; 82 LOG(ERROR) << "Write(): file too large";
82 return false; 83 return false;
83 } 84 }
84 85
85 string_.replace(offset, size, reinterpret_cast<const char*>(data), size); 86 string_.replace(offset, size, reinterpret_cast<const char*>(data), size);
86 offset_ = new_offset; 87 offset_ = new_offset;
87 88
88 return true; 89 return true;
89 } 90 }
90 91
91 bool StringFile::WriteIoVec(std::vector<WritableIoVec>* iovecs) { 92 bool StringFile::WriteIoVec(std::vector<WritableIoVec>* iovecs) {
92 DCHECK(offset_.IsValid()); 93 DCHECK(offset_.IsValid());
93 94
94 if (iovecs->empty()) { 95 if (iovecs->empty()) {
95 LOG(ERROR) << "WriteIoVec(): no iovecs"; 96 LOG(ERROR) << "WriteIoVec(): no iovecs";
96 return false; 97 return false;
97 } 98 }
98 99
99 // Avoid writing anything at all if it would cause an overflow. 100 // Avoid writing anything at all if it would cause an overflow.
100 base::CheckedNumeric<ssize_t> new_offset = offset_; 101 base::CheckedNumeric<FileOperationResult> new_offset = offset_;
101 for (const WritableIoVec& iov : *iovecs) { 102 for (const WritableIoVec& iov : *iovecs) {
102 new_offset += iov.iov_len; 103 new_offset += iov.iov_len;
103 if (!new_offset.IsValid()) { 104 if (!new_offset.IsValid()) {
104 LOG(ERROR) << "WriteIoVec(): file too large"; 105 LOG(ERROR) << "WriteIoVec(): file too large";
105 return false; 106 return false;
106 } 107 }
107 } 108 }
108 109
109 for (const WritableIoVec& iov : *iovecs) { 110 for (const WritableIoVec& iov : *iovecs) {
110 if (!Write(iov.iov_base, iov.iov_len)) { 111 if (!Write(iov.iov_base, iov.iov_len)) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 << " invalid for size_t"; 164 << " invalid for size_t";
164 return -1; 165 return -1;
165 } 166 }
166 167
167 offset_ = new_offset_sizet; 168 offset_ = new_offset_sizet;
168 169
169 return offset_.ValueOrDie(); 170 return offset_.ValueOrDie();
170 } 171 }
171 172
172 } // namespace crashpad 173 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/file/string_file.h ('k') | util/file/string_file_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698