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

Side by Side Diff: components/policy/core/common/preg_parser.cc

Issue 2622583002: Push preg_parser changes upstream (Closed)
Patch Set: Changed NextChar back to int (-1 is error code) Created 3 years, 11 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/policy/core/common/preg_parser.h" 5 #include "components/policy/core/common/preg_parser.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
11 #include <functional> 11 #include <functional>
12 #include <iterator> 12 #include <iterator>
13 #include <limits>
13 #include <memory> 14 #include <memory>
14 #include <string> 15 #include <string>
15 #include <utility> 16 #include <utility>
16 #include <vector> 17 #include <vector>
17 18
18 #include "base/files/file_path.h" 19 #include "base/files/file_path.h"
19 #include "base/files/memory_mapped_file.h" 20 #include "base/files/memory_mapped_file.h"
20 #include "base/logging.h" 21 #include "base/logging.h"
21 #include "base/macros.h" 22 #include "base/macros.h"
22 #include "base/memory/ptr_util.h" 23 #include "base/memory/ptr_util.h"
(...skipping 23 matching lines...) Expand all
46 #define REG_RESOURCE_REQUIREMENTS_LIST 10 47 #define REG_RESOURCE_REQUIREMENTS_LIST 10
47 #define REG_QWORD_LITTLE_ENDIAN 11 48 #define REG_QWORD_LITTLE_ENDIAN 11
48 #endif 49 #endif
49 50
50 using RegistryDict = policy::RegistryDict; 51 using RegistryDict = policy::RegistryDict;
51 52
52 namespace { 53 namespace {
53 54
54 // Maximum PReg file size we're willing to accept. 55 // Maximum PReg file size we're willing to accept.
55 const int64_t kMaxPRegFileSize = 1024 * 1024 * 16; 56 const int64_t kMaxPRegFileSize = 1024 * 1024 * 16;
57 static_assert(kMaxPRegFileSize <= std::numeric_limits<ptrdiff_t>::max(),
58 "Max PReg file size too large.");
56 59
57 // Constants for PReg file delimiters. 60 // Constants for PReg file delimiters.
58 const base::char16 kDelimBracketOpen = L'['; 61 const base::char16 kDelimBracketOpen = L'[';
59 const base::char16 kDelimBracketClose = L']'; 62 const base::char16 kDelimBracketClose = L']';
60 const base::char16 kDelimSemicolon = L';'; 63 const base::char16 kDelimSemicolon = L';';
61 64
62 // Registry path separator. 65 // Registry path separator.
63 const base::string16 kRegistryPathSeparator = base::ASCIIToUTF16("\\"); 66 const base::string16 kRegistryPathSeparator = base::ASCIIToUTF16("\\");
64 67
65 // Magic strings for the PReg value field to trigger special actions. 68 // Magic strings for the PReg value field to trigger special actions.
66 const char kActionTriggerPrefix[] = "**"; 69 const char kActionTriggerPrefix[] = "**";
67 const char kActionTriggerDeleteValues[] = "deletevalues"; 70 const char kActionTriggerDeleteValues[] = "deletevalues";
68 const char kActionTriggerDel[] = "del."; 71 const char kActionTriggerDel[] = "del.";
69 const char kActionTriggerDelVals[] = "delvals"; 72 const char kActionTriggerDelVals[] = "delvals";
70 const char kActionTriggerDeleteKeys[] = "deletekeys"; 73 const char kActionTriggerDeleteKeys[] = "deletekeys";
71 const char kActionTriggerSecureKey[] = "securekey"; 74 const char kActionTriggerSecureKey[] = "securekey";
72 const char kActionTriggerSoft[] = "soft"; 75 const char kActionTriggerSoft[] = "soft";
73 76
74 // Returns the character at |cursor| and increments it, unless the end is here 77 // Returns the character at |cursor| and increments it, unless the end is here
75 // in which case -1 is returned. 78 // in which case -1 is returned. The calling code must guarantee that
79 // end - *cursor does not overflow ptrdiff_t.
76 int NextChar(const uint8_t** cursor, const uint8_t* end) { 80 int NextChar(const uint8_t** cursor, const uint8_t* end) {
77 // Only read the character if a full base::char16 is available. 81 // Only read the character if a full base::char16 is available.
78 // This comparison makes sure no overflow can happen. 82 // This comparison makes sure no overflow can happen.
79 if (*cursor >= end || 83 if (*cursor >= end ||
80 static_cast<size_t>(end - *cursor) < sizeof(base::char16)) 84 end - *cursor < static_cast<ptrdiff_t>(sizeof(base::char16)))
81 return -1; 85 return -1;
82 86
83 int result = **cursor | (*(*cursor + 1) << 8); 87 int result = **cursor | (*(*cursor + 1) << 8);
84 *cursor += sizeof(base::char16); 88 *cursor += sizeof(base::char16);
85 return result; 89 return result;
86 } 90 }
87 91
88 // Reads a fixed-size field from a PReg file. 92 // Reads a fixed-size field from a PReg file. The calling code must guarantee
93 // that both end - *cursor and size do not overflow ptrdiff_t.
89 bool ReadFieldBinary(const uint8_t** cursor, 94 bool ReadFieldBinary(const uint8_t** cursor,
90 const uint8_t* end, 95 const uint8_t* end,
91 uint32_t size, 96 uint32_t size,
92 uint8_t* data) { 97 uint8_t* data) {
93 if (size == 0) 98 if (size == 0)
94 return true; 99 return true;
95 100
96 // Be careful to prevent possible overflows here (don't do *cursor + size). 101 // Be careful to prevent possible overflows here (don't do *cursor + size).
97 if (*cursor >= end || static_cast<size_t>(end - *cursor) < size) 102 if (*cursor >= end || end - *cursor < static_cast<ptrdiff_t>(size))
98 return false; 103 return false;
99 const uint8_t* field_end = *cursor + size; 104 const uint8_t* field_end = *cursor + size;
100 std::copy(*cursor, field_end, data); 105 std::copy(*cursor, field_end, data);
101 *cursor = field_end; 106 *cursor = field_end;
102 return true; 107 return true;
103 } 108 }
104 109
105 bool ReadField32(const uint8_t** cursor, const uint8_t* end, uint32_t* data) { 110 bool ReadField32(const uint8_t** cursor, const uint8_t* end, uint32_t* data) {
106 uint32_t value = 0; 111 uint32_t value = 0;
107 if (!ReadFieldBinary(cursor, end, sizeof(uint32_t), 112 if (!ReadFieldBinary(cursor, end, sizeof(uint32_t),
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 343
339 LOG(ERROR) << "Error parsing " << file_path.value() << " at offset " 344 LOG(ERROR) << "Error parsing " << file_path.value() << " at offset "
340 << reinterpret_cast<const uint8_t*>(cursor - 1) - 345 << reinterpret_cast<const uint8_t*>(cursor - 1) -
341 mapped_file.data(); 346 mapped_file.data();
342 status->Add(POLICY_LOAD_STATUS_PARSE_ERROR); 347 status->Add(POLICY_LOAD_STATUS_PARSE_ERROR);
343 return false; 348 return false;
344 } 349 }
345 350
346 } // namespace preg_parser 351 } // namespace preg_parser
347 } // namespace policy 352 } // namespace policy
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698