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

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

Issue 1505213004: Copy Crashpad into the Chrome tree instead of importing it via DEPS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update Created 5 years 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
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/file/string_file.h"
16
17 #include <string.h>
18
19 #include <algorithm>
20 #include <limits>
21
22 #include "base/logging.h"
23 #include "base/numerics/safe_math.h"
24 #include "util/misc/implicit_cast.h"
25 #include "util/numeric/safe_assignment.h"
26
27 namespace crashpad {
28
29 StringFile::StringFile() : string_(), offset_(0) {
30 }
31
32 StringFile::~StringFile() {
33 }
34
35 void StringFile::SetString(const std::string& string) {
36 CHECK_LE(
37 string.size(),
38 implicit_cast<size_t>(std::numeric_limits<FileOperationResult>::max()));
39 string_ = string;
40 offset_ = 0;
41 }
42
43 void StringFile::Reset() {
44 string_.clear();
45 offset_ = 0;
46 }
47
48 FileOperationResult StringFile::Read(void* data, size_t size) {
49 DCHECK(offset_.IsValid());
50
51 const size_t offset = offset_.ValueOrDie();
52 if (offset >= string_.size()) {
53 return 0;
54 }
55
56 const size_t nread = std::min(size, string_.size() - offset);
57
58 base::CheckedNumeric<FileOperationResult> new_offset = offset_;
59 new_offset += nread;
60 if (!new_offset.IsValid()) {
61 LOG(ERROR) << "Read(): file too large";
62 return -1;
63 }
64
65 memcpy(data, &string_[offset], nread);
66 offset_ = new_offset;
67
68 return nread;
69 }
70
71 bool StringFile::Write(const void* data, size_t size) {
72 DCHECK(offset_.IsValid());
73
74 const size_t offset = offset_.ValueOrDie();
75 if (offset > string_.size()) {
76 string_.resize(offset);
77 }
78
79 base::CheckedNumeric<FileOperationResult> new_offset = offset_;
80 new_offset += size;
81 if (!new_offset.IsValid()) {
82 LOG(ERROR) << "Write(): file too large";
83 return false;
84 }
85
86 string_.replace(offset, size, reinterpret_cast<const char*>(data), size);
87 offset_ = new_offset;
88
89 return true;
90 }
91
92 bool StringFile::WriteIoVec(std::vector<WritableIoVec>* iovecs) {
93 DCHECK(offset_.IsValid());
94
95 if (iovecs->empty()) {
96 LOG(ERROR) << "WriteIoVec(): no iovecs";
97 return false;
98 }
99
100 // Avoid writing anything at all if it would cause an overflow.
101 base::CheckedNumeric<FileOperationResult> new_offset = offset_;
102 for (const WritableIoVec& iov : *iovecs) {
103 new_offset += iov.iov_len;
104 if (!new_offset.IsValid()) {
105 LOG(ERROR) << "WriteIoVec(): file too large";
106 return false;
107 }
108 }
109
110 for (const WritableIoVec& iov : *iovecs) {
111 if (!Write(iov.iov_base, iov.iov_len)) {
112 return false;
113 }
114 }
115
116 #ifndef NDEBUG
117 // The interface says that |iovecs| is not sacred, so scramble it to make sure
118 // that nobody depends on it.
119 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size());
120 #endif
121
122 return true;
123 }
124
125 FileOffset StringFile::Seek(FileOffset offset, int whence) {
126 DCHECK(offset_.IsValid());
127
128 size_t base_offset;
129
130 switch (whence) {
131 case SEEK_SET:
132 base_offset = 0;
133 break;
134
135 case SEEK_CUR:
136 base_offset = offset_.ValueOrDie();
137 break;
138
139 case SEEK_END:
140 base_offset = string_.size();
141 break;
142
143 default:
144 LOG(ERROR) << "Seek(): invalid whence " << whence;
145 return -1;
146 }
147
148 FileOffset base_offset_fileoffset;
149 if (!AssignIfInRange(&base_offset_fileoffset, base_offset)) {
150 LOG(ERROR) << "Seek(): base_offset " << base_offset
151 << " invalid for FileOffset";
152 return -1;
153 }
154 base::CheckedNumeric<FileOffset> new_offset(base_offset_fileoffset);
155 new_offset += offset;
156 if (!new_offset.IsValid()) {
157 LOG(ERROR) << "Seek(): new_offset invalid";
158 return -1;
159 }
160 FileOffset new_offset_fileoffset = new_offset.ValueOrDie();
161 size_t new_offset_sizet;
162 if (!AssignIfInRange(&new_offset_sizet, new_offset_fileoffset)) {
163 LOG(ERROR) << "Seek(): new_offset " << new_offset_fileoffset
164 << " invalid for size_t";
165 return -1;
166 }
167
168 offset_ = new_offset_sizet;
169
170 return offset_.ValueOrDie();
171 }
172
173 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698