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

Unified Diff: util/file/string_file_writer.cc

Issue 432003005: Introduce MinidumpWritable, its dependencies, and their tests (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Address review comments Created 6 years, 5 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « util/file/string_file_writer.h ('k') | util/file/string_file_writer_test.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: util/file/string_file_writer.cc
diff --git a/util/file/string_file_writer.cc b/util/file/string_file_writer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2c2f1f5236be84d7aa373801707dee2b93885b63
--- /dev/null
+++ b/util/file/string_file_writer.cc
@@ -0,0 +1,137 @@
+// Copyright 2014 The Crashpad Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "util/file/string_file_writer.h"
+
+#include <string.h>
+
+#include "base/logging.h"
+#include "base/numerics/safe_math.h"
+#include "util/numeric/safe_assignment.h"
+
+namespace crashpad {
+
+StringFileWriter::StringFileWriter() : string_(), offset_(0) {
+}
+
+StringFileWriter::~StringFileWriter() {
+}
+
+void StringFileWriter::Reset() {
+ string_.clear();
+ offset_ = 0;
+}
+
+bool StringFileWriter::Write(const void* data, size_t size) {
+ DCHECK(offset_.IsValid());
+
+ const size_t offset = offset_.ValueOrDie();
+ if (offset > string_.size()) {
+ string_.resize(offset);
+ }
+
+ base::CheckedNumeric<ssize_t> new_offset = offset_;
+ new_offset += size;
+ if (!new_offset.IsValid()) {
+ LOG(ERROR) << "Write(): file too large";
+ return false;
+ }
+
+ string_.replace(offset, size, reinterpret_cast<const char*>(data), size);
+ offset_ = new_offset;
+
+ return true;
+}
+
+bool StringFileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) {
+ DCHECK(offset_.IsValid());
+
+ if (iovecs->empty()) {
+ LOG(ERROR) << "WriteIoVec(): no iovecs";
+ return false;
+ }
+
+ // Avoid writing anything at all if it would cause an overflow.
+ base::CheckedNumeric<ssize_t> new_offset = offset_;
+ for (const WritableIoVec& iov : *iovecs) {
+ new_offset += iov.iov_len;
+ if (!new_offset.IsValid()) {
+ LOG(ERROR) << "WriteIoVec(): file too large";
+ return false;
+ }
+ }
+
+ for (const WritableIoVec& iov : *iovecs) {
+ if (!Write(iov.iov_base, iov.iov_len)) {
+ return false;
+ }
+ }
+
+#ifndef NDEBUG
+ // The interface says that |iovecs| is not sacred, so scramble it to make sure
+ // that nobody depends on it.
+ memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size());
+#endif
+
+ return true;
+}
+
+off_t StringFileWriter::Seek(off_t offset, int whence) {
+ DCHECK(offset_.IsValid());
+
+ size_t base_offset;
+
+ switch (whence) {
+ case SEEK_SET:
+ base_offset = 0;
+ break;
+
+ case SEEK_CUR:
+ base_offset = offset_.ValueOrDie();
+ break;
+
+ case SEEK_END:
+ base_offset = string_.size();
+ break;
+
+ default:
+ LOG(ERROR) << "Seek(): invalid whence " << whence;
+ return -1;
+ }
+
+ off_t offset_offt;
+ if (!AssignIfInRange(&offset_offt, base_offset)) {
+ LOG(ERROR) << "Seek(): base_offset " << base_offset << " invalid for off_t";
+ return -1;
+ }
+
+ base::CheckedNumeric<off_t> new_offset(offset_offt);
+ new_offset += offset;
+ if (!new_offset.IsValid()) {
+ LOG(ERROR) << "Seek(): new_offset invalid";
+ return -1;
+ }
+
+ if (!AssignIfInRange(&offset_offt, new_offset.ValueOrDie())) {
+ LOG(ERROR) << "Seek(): new_offset " << new_offset.ValueOrDie()
+ << " invalid for size_t";
+ return -1;
+ }
+
+ offset_ = offset_offt;
+
+ return offset_.ValueOrDie();
+}
+
+} // namespace crashpad
« no previous file with comments | « util/file/string_file_writer.h ('k') | util/file/string_file_writer_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698