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 "util/file/fd_io.h" | 15 #include "util/file/fd_io.h" |
16 | 16 |
17 #include <unistd.h> | 17 #include <unistd.h> |
18 | 18 |
| 19 #include "base/logging.h" |
| 20 #include "base/numerics/safe_conversions.h" |
19 #include "base/posix/eintr_wrapper.h" | 21 #include "base/posix/eintr_wrapper.h" |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 struct ReadTraits { | 25 struct ReadTraits { |
24 typedef void* VoidBufferType; | 26 typedef void* VoidBufferType; |
25 typedef char* CharBufferType; | 27 typedef char* CharBufferType; |
26 static ssize_t Operate(int fd, CharBufferType buffer, size_t size) { | 28 static ssize_t Operate(int fd, CharBufferType buffer, size_t size) { |
27 return read(fd, buffer, size); | 29 return read(fd, buffer, size); |
28 } | 30 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 namespace crashpad { | 67 namespace crashpad { |
66 | 68 |
67 ssize_t ReadFD(int fd, void* buffer, size_t size) { | 69 ssize_t ReadFD(int fd, void* buffer, size_t size) { |
68 return ReadOrWrite<ReadTraits>(fd, buffer, size); | 70 return ReadOrWrite<ReadTraits>(fd, buffer, size); |
69 } | 71 } |
70 | 72 |
71 ssize_t WriteFD(int fd, const void* buffer, size_t size) { | 73 ssize_t WriteFD(int fd, const void* buffer, size_t size) { |
72 return ReadOrWrite<WriteTraits>(fd, buffer, size); | 74 return ReadOrWrite<WriteTraits>(fd, buffer, size); |
73 } | 75 } |
74 | 76 |
| 77 void CheckedReadFD(int fd, void* buffer, size_t size) { |
| 78 ssize_t expect = base::checked_cast<ssize_t>(size); |
| 79 ssize_t rv = ReadFD(fd, buffer, size); |
| 80 if (rv < 0) { |
| 81 PCHECK(rv == expect) << "read"; |
| 82 } else { |
| 83 CHECK_EQ(rv, expect) << "read"; |
| 84 } |
| 85 } |
| 86 |
| 87 void CheckedWriteFD(int fd, const void* buffer, size_t size) { |
| 88 ssize_t expect = base::checked_cast<ssize_t>(size); |
| 89 ssize_t rv = WriteFD(fd, buffer, size); |
| 90 if (rv < 0) { |
| 91 PCHECK(rv == expect) << "write"; |
| 92 } else { |
| 93 CHECK_EQ(rv, expect) << "write"; |
| 94 } |
| 95 } |
| 96 |
| 97 void CheckedReadFDAtEOF(int fd) { |
| 98 char c; |
| 99 ssize_t rv = ReadFD(fd, &c, 1); |
| 100 if (rv < 0) { |
| 101 PCHECK(rv == 0) << "read"; |
| 102 } else { |
| 103 CHECK_EQ(rv, 0) << "read"; |
| 104 } |
| 105 } |
| 106 |
75 } // namespace crashpad | 107 } // namespace crashpad |
OLD | NEW |