OLD | NEW |
(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_writer.h" |
| 16 |
| 17 #include <string.h> |
| 18 |
| 19 #include "base/logging.h" |
| 20 #include "base/numerics/safe_math.h" |
| 21 #include "util/numeric/safe_assignment.h" |
| 22 |
| 23 namespace crashpad { |
| 24 |
| 25 StringFileWriter::StringFileWriter() : string_(), offset_(0) { |
| 26 } |
| 27 |
| 28 StringFileWriter::~StringFileWriter() { |
| 29 } |
| 30 |
| 31 void StringFileWriter::Reset() { |
| 32 string_.clear(); |
| 33 offset_ = 0; |
| 34 } |
| 35 |
| 36 bool StringFileWriter::Write(const void* data, size_t size) { |
| 37 DCHECK(offset_.IsValid()); |
| 38 |
| 39 const size_t offset = offset_.ValueOrDie(); |
| 40 if (offset > string_.size()) { |
| 41 string_.resize(offset); |
| 42 } |
| 43 |
| 44 base::CheckedNumeric<ssize_t> new_offset = offset_; |
| 45 new_offset += size; |
| 46 if (!new_offset.IsValid()) { |
| 47 LOG(ERROR) << "Write(): file too large"; |
| 48 return false; |
| 49 } |
| 50 |
| 51 string_.replace(offset, size, reinterpret_cast<const char*>(data), size); |
| 52 offset_ = new_offset; |
| 53 |
| 54 return true; |
| 55 } |
| 56 |
| 57 bool StringFileWriter::WriteIoVec(std::vector<WritableIoVec>* iovecs) { |
| 58 DCHECK(offset_.IsValid()); |
| 59 |
| 60 if (iovecs->empty()) { |
| 61 LOG(ERROR) << "WriteIoVec(): no iovecs"; |
| 62 return false; |
| 63 } |
| 64 |
| 65 // Avoid writing anything at all if it would cause an overflow. |
| 66 base::CheckedNumeric<ssize_t> new_offset = offset_; |
| 67 for (const WritableIoVec& iov : *iovecs) { |
| 68 new_offset += iov.iov_len; |
| 69 if (!new_offset.IsValid()) { |
| 70 LOG(ERROR) << "WriteIoVec(): file too large"; |
| 71 return false; |
| 72 } |
| 73 } |
| 74 |
| 75 for (const WritableIoVec& iov : *iovecs) { |
| 76 if (!Write(iov.iov_base, iov.iov_len)) { |
| 77 return false; |
| 78 } |
| 79 } |
| 80 |
| 81 #ifndef NDEBUG |
| 82 // The interface says that |iovecs| is not sacred, so scramble it to make sure |
| 83 // that nobody depends on it. |
| 84 memset(&(*iovecs)[0], 0xa5, sizeof((*iovecs)[0]) * iovecs->size()); |
| 85 #endif |
| 86 |
| 87 return true; |
| 88 } |
| 89 |
| 90 off_t StringFileWriter::Seek(off_t offset, int whence) { |
| 91 DCHECK(offset_.IsValid()); |
| 92 |
| 93 size_t base_offset; |
| 94 |
| 95 switch (whence) { |
| 96 case SEEK_SET: |
| 97 base_offset = 0; |
| 98 break; |
| 99 |
| 100 case SEEK_CUR: |
| 101 base_offset = offset_.ValueOrDie(); |
| 102 break; |
| 103 |
| 104 case SEEK_END: |
| 105 base_offset = string_.size(); |
| 106 break; |
| 107 |
| 108 default: |
| 109 LOG(ERROR) << "Seek(): invalid whence " << whence; |
| 110 return -1; |
| 111 } |
| 112 |
| 113 off_t offset_offt; |
| 114 if (!AssignIfInRange(&offset_offt, base_offset)) { |
| 115 LOG(ERROR) << "Seek(): base_offset " << base_offset << " invalid for off_t"; |
| 116 return -1; |
| 117 } |
| 118 |
| 119 base::CheckedNumeric<off_t> new_offset(offset_offt); |
| 120 new_offset += offset; |
| 121 if (!new_offset.IsValid()) { |
| 122 LOG(ERROR) << "Seek(): new_offset invalid"; |
| 123 return -1; |
| 124 } |
| 125 |
| 126 if (!AssignIfInRange(&offset_offt, new_offset.ValueOrDie())) { |
| 127 LOG(ERROR) << "Seek(): new_offset " << new_offset.ValueOrDie() |
| 128 << " invalid for size_t"; |
| 129 return -1; |
| 130 } |
| 131 |
| 132 offset_ = offset_offt; |
| 133 |
| 134 return offset_.ValueOrDie(); |
| 135 } |
| 136 |
| 137 } // namespace crashpad |
OLD | NEW |