| OLD | NEW |
| 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, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "minidump/minidump_string_writer.h" | 15 #include "minidump/minidump_string_writer.h" |
| 16 | 16 |
| 17 #include <sys/types.h> | 17 #include <sys/types.h> |
| 18 | 18 |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "minidump/minidump_writer_util.h" | 20 #include "minidump/minidump_writer_util.h" |
| 21 #include "util/file/file_writer.h" | 21 #include "util/file/file_writer.h" |
| 22 #include "util/numeric/safe_assignment.h" | 22 #include "util/numeric/safe_assignment.h" |
| 23 | 23 |
| 24 namespace crashpad { | 24 namespace crashpad { |
| 25 namespace internal { | 25 namespace internal { |
| 26 | 26 |
| 27 template <typename Traits> | 27 template <typename Traits> |
| 28 MinidumpStringWriter<Traits>::MinidumpStringWriter() | 28 MinidumpStringWriter<Traits>::MinidumpStringWriter() |
| 29 : MinidumpWritable(), string_base_(), string_() { | 29 : MinidumpWritable(), string_base_(new MinidumpStringType()), string_() { |
| 30 } | 30 } |
| 31 | 31 |
| 32 template <typename Traits> | 32 template <typename Traits> |
| 33 MinidumpStringWriter<Traits>::~MinidumpStringWriter() { | 33 MinidumpStringWriter<Traits>::~MinidumpStringWriter() { |
| 34 } | 34 } |
| 35 | 35 |
| 36 template <typename Traits> | 36 template <typename Traits> |
| 37 bool MinidumpStringWriter<Traits>::Freeze() { | 37 bool MinidumpStringWriter<Traits>::Freeze() { |
| 38 DCHECK_EQ(state(), kStateMutable); | 38 DCHECK_EQ(state(), kStateMutable); |
| 39 | 39 |
| 40 if (!MinidumpWritable::Freeze()) { | 40 if (!MinidumpWritable::Freeze()) { |
| 41 return false; | 41 return false; |
| 42 } | 42 } |
| 43 | 43 |
| 44 size_t string_bytes = string_.size() * sizeof(string_[0]); | 44 size_t string_bytes = string_.size() * sizeof(string_[0]); |
| 45 if (!AssignIfInRange(&string_base_.Length, string_bytes)) { | 45 if (!AssignIfInRange(&string_base_->Length, string_bytes)) { |
| 46 LOG(ERROR) << "string_bytes " << string_bytes << " out of range"; | 46 LOG(ERROR) << "string_bytes " << string_bytes << " out of range"; |
| 47 return false; | 47 return false; |
| 48 } | 48 } |
| 49 | 49 |
| 50 return true; | 50 return true; |
| 51 } | 51 } |
| 52 | 52 |
| 53 template <typename Traits> | 53 template <typename Traits> |
| 54 size_t MinidumpStringWriter<Traits>::SizeOfObject() { | 54 size_t MinidumpStringWriter<Traits>::SizeOfObject() { |
| 55 DCHECK_GE(state(), kStateFrozen); | 55 DCHECK_GE(state(), kStateFrozen); |
| 56 | 56 |
| 57 // Include the NUL terminator. | 57 // Include the NUL terminator. |
| 58 return sizeof(string_base_) + (string_.size() + 1) * sizeof(string_[0]); | 58 return sizeof(*string_base_) + (string_.size() + 1) * sizeof(string_[0]); |
| 59 } | 59 } |
| 60 | 60 |
| 61 template <typename Traits> | 61 template <typename Traits> |
| 62 bool MinidumpStringWriter<Traits>::WriteObject( | 62 bool MinidumpStringWriter<Traits>::WriteObject( |
| 63 FileWriterInterface* file_writer) { | 63 FileWriterInterface* file_writer) { |
| 64 DCHECK_EQ(state(), kStateWritable); | 64 DCHECK_EQ(state(), kStateWritable); |
| 65 | 65 |
| 66 // The string’s length is stored in string_base_, and its data is stored in | 66 // The string’s length is stored in string_base_, and its data is stored in |
| 67 // string_. Write them both. | 67 // string_. Write them both. |
| 68 WritableIoVec iov; | 68 WritableIoVec iov; |
| 69 iov.iov_base = &string_base_; | 69 iov.iov_base = string_base_.get(); |
| 70 iov.iov_len = sizeof(string_base_); | 70 iov.iov_len = sizeof(*string_base_); |
| 71 std::vector<WritableIoVec> iovecs(1, iov); | 71 std::vector<WritableIoVec> iovecs(1, iov); |
| 72 | 72 |
| 73 // Include the NUL terminator. | 73 // Include the NUL terminator. |
| 74 iov.iov_base = &string_[0]; | 74 iov.iov_base = &string_[0]; |
| 75 iov.iov_len = (string_.size() + 1) * sizeof(string_[0]); | 75 iov.iov_len = (string_.size() + 1) * sizeof(string_[0]); |
| 76 iovecs.push_back(iov); | 76 iovecs.push_back(iov); |
| 77 | 77 |
| 78 return file_writer->WriteIoVec(&iovecs); | 78 return file_writer->WriteIoVec(&iovecs); |
| 79 } | 79 } |
| 80 | 80 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 return !IsEmpty(); | 129 return !IsEmpty(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Explicit template instantiation of the forms of MinidumpStringListWriter<> | 132 // Explicit template instantiation of the forms of MinidumpStringListWriter<> |
| 133 // used as type aliases. | 133 // used as type aliases. |
| 134 template class MinidumpStringListWriter<MinidumpUTF16StringWriter>; | 134 template class MinidumpStringListWriter<MinidumpUTF16StringWriter>; |
| 135 template class MinidumpStringListWriter<MinidumpUTF8StringWriter>; | 135 template class MinidumpStringListWriter<MinidumpUTF8StringWriter>; |
| 136 | 136 |
| 137 } // namespace internal | 137 } // namespace internal |
| 138 } // namespace crashpad | 138 } // namespace crashpad |
| OLD | NEW |