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_writer_util.h" | 15 #include "util/file/file_io.h" |
16 | 16 |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 |
20 #include "util/stdlib/strlcpy.h" | 20 namespace { |
| 21 |
| 22 bool IsSocketHandle(HANDLE file) { |
| 23 if (GetFileType(file) == FILE_TYPE_PIPE) { |
| 24 // FILE_TYPE_PIPE means that it's a socket, a named pipe, or an anonymous |
| 25 // pipe. If we are unable to retrieve the pipe information, we know it's a |
| 26 // socket. |
| 27 return !GetNamedPipeInfo(file, NULL, NULL, NULL, NULL); |
| 28 } |
| 29 return false; |
| 30 } |
| 31 |
| 32 } // namespace |
21 | 33 |
22 namespace crashpad { | 34 namespace crashpad { |
23 namespace internal { | |
24 | 35 |
25 // static | 36 // TODO(scottmg): Handle > DWORD sized writes if necessary. |
26 void MinidumpWriterUtil::AssignTimeT(uint32_t* destination, time_t source) { | 37 |
27 if (!base::IsValueInRangeForNumericType<uint32_t>(source)) { | 38 ssize_t ReadFile(FileHandle file, void* buffer, size_t size) { |
28 LOG(WARNING) << "timestamp " << source << " out of range"; | 39 DCHECK(!IsSocketHandle(file)); |
| 40 DWORD size_dword = base::checked_cast<DWORD>(size); |
| 41 DWORD total_read = 0; |
| 42 char* buffer_c = reinterpret_cast<char*>(buffer); |
| 43 while (size_dword > 0) { |
| 44 DWORD bytes_read; |
| 45 BOOL success = ::ReadFile(file, buffer_c, size_dword, &bytes_read, nullptr); |
| 46 if (!success && GetLastError() != ERROR_MORE_DATA) { |
| 47 return -1; |
| 48 } else if (success && bytes_read == 0 && |
| 49 GetFileType(file) != FILE_TYPE_PIPE) { |
| 50 // Zero bytes read for a file indicates reaching EOF. Zero bytes read from |
| 51 // a pipe indicates only that there was a zero byte WriteFile issued on |
| 52 // the other end, so continue reading. |
| 53 break; |
| 54 } |
| 55 |
| 56 buffer_c += bytes_read; |
| 57 size_dword -= bytes_read; |
| 58 total_read += bytes_read; |
29 } | 59 } |
30 | 60 return total_read; |
31 *destination = source; | |
32 } | 61 } |
33 | 62 |
34 // static | 63 ssize_t WriteFile(FileHandle file, const void* buffer, size_t size) { |
35 base::string16 MinidumpWriterUtil::ConvertUTF8ToUTF16(const std::string& utf8) { | 64 // TODO(scottmg): This might need to handle the limit for pipes across a |
36 base::string16 utf16; | 65 // network in the future. |
37 if (!base::UTF8ToUTF16(utf8.data(), utf8.length(), &utf16)) { | 66 DWORD size_dword = base::checked_cast<DWORD>(size); |
38 LOG(WARNING) << "string " << utf8 | 67 DWORD bytes_written; |
39 << " cannot be converted to UTF-16 losslessly"; | 68 BOOL rv = ::WriteFile(file, buffer, size_dword, &bytes_written, nullptr); |
40 } | 69 if (!rv) |
41 return utf16; | 70 return -1; |
| 71 CHECK_EQ(bytes_written, size_dword); |
| 72 return bytes_written; |
42 } | 73 } |
43 | 74 |
44 // static | 75 bool LoggingCloseFile(FileHandle file) { |
45 void MinidumpWriterUtil::AssignUTF8ToUTF16(base::char16* destination, | 76 BOOL rv = CloseHandle(file); |
46 size_t destination_size, | 77 PLOG_IF(ERROR, !rv) << "CloseHandle"; |
47 const std::string& source) { | 78 return rv; |
48 base::string16 source_utf16 = ConvertUTF8ToUTF16(source); | |
49 if (source_utf16.size() > destination_size - 1) { | |
50 LOG(WARNING) << "string " << source << " UTF-16 length " | |
51 << source_utf16.size() | |
52 << " will be truncated to UTF-16 length " | |
53 << destination_size - 1; | |
54 } | |
55 | |
56 source_utf16.resize(destination_size - 1); | |
57 c16lcpy(destination, source_utf16.c_str(), destination_size); | |
58 } | 79 } |
59 | 80 |
60 } // namespace internal | |
61 } // namespace crashpad | 81 } // namespace crashpad |
OLD | NEW |