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

Side by Side Diff: net/base/ip_address.cc

Issue 1828103002: Extend net/base/parse_number.h for parsing of negative numbers, and determining if there was overflo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proxy_num
Patch Set: fix comments Created 4 years, 8 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 | « net/base/host_port_pair.cc ('k') | net/base/parse_number.h » ('j') | 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) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 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 "net/base/ip_address.h" 5 #include "net/base/ip_address.h"
6 6
7 #include "base/strings/string_piece.h" 7 #include "base/strings/string_piece.h"
8 #include "base/strings/string_split.h" 8 #include "base/strings/string_split.h"
9 #include "net/base/ip_address_number.h" 9 #include "net/base/ip_address_number.h"
10 #include "net/base/parse_number.h" 10 #include "net/base/parse_number.h"
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 std::vector<base::StringPiece> parts = base::SplitStringPiece( 173 std::vector<base::StringPiece> parts = base::SplitStringPiece(
174 cidr_literal, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); 174 cidr_literal, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
175 if (parts.size() != 2) 175 if (parts.size() != 2)
176 return false; 176 return false;
177 177
178 // Parse the IP address. 178 // Parse the IP address.
179 if (!ip_address->AssignFromIPLiteral(parts[0])) 179 if (!ip_address->AssignFromIPLiteral(parts[0]))
180 return false; 180 return false;
181 181
182 // Parse the prefix length. 182 // Parse the prefix length.
183 int number_of_bits = -1; 183 uint32_t number_of_bits;
184 if (!ParseNonNegativeDecimalInt(parts[1], &number_of_bits)) 184 if (!ParseUint32(parts[1], &number_of_bits))
185 return false; 185 return false;
186 186
187 // Make sure the prefix length is in a valid range. 187 // Make sure the prefix length is in a valid range.
188 if (number_of_bits < 0 || 188 if (number_of_bits > ip_address->size() * 8)
189 number_of_bits > static_cast<int>(ip_address->size() * 8))
190 return false; 189 return false;
191 190
192 *prefix_length_in_bits = static_cast<size_t>(number_of_bits); 191 *prefix_length_in_bits = number_of_bits;
193 return true; 192 return true;
194 } 193 }
195 194
196 bool ParseURLHostnameToAddress(const std::string& hostname, 195 bool ParseURLHostnameToAddress(const std::string& hostname,
197 IPAddress* ip_address) { 196 IPAddress* ip_address) {
198 if (hostname.size() >= 2 && hostname.front() == '[' && 197 if (hostname.size() >= 2 && hostname.front() == '[' &&
199 hostname.back() == ']') { 198 hostname.back() == ']') {
200 // Strip the square brackets that surround IPv6 literals. 199 // Strip the square brackets that surround IPv6 literals.
201 auto ip_literal = 200 auto ip_literal =
202 base::StringPiece(hostname).substr(1, hostname.size() - 2); 201 base::StringPiece(hostname).substr(1, hostname.size() - 2);
203 return ip_address->AssignFromIPLiteral(ip_literal) && ip_address->IsIPv6(); 202 return ip_address->AssignFromIPLiteral(ip_literal) && ip_address->IsIPv6();
204 } 203 }
205 204
206 return ip_address->AssignFromIPLiteral(hostname) && ip_address->IsIPv4(); 205 return ip_address->AssignFromIPLiteral(hostname) && ip_address->IsIPv4();
207 } 206 }
208 207
209 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) { 208 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) {
210 return CommonPrefixLength(a1.bytes(), a2.bytes()); 209 return CommonPrefixLength(a1.bytes(), a2.bytes());
211 } 210 }
212 211
213 unsigned MaskPrefixLength(const IPAddress& mask) { 212 unsigned MaskPrefixLength(const IPAddress& mask) {
214 return MaskPrefixLength(mask.bytes()); 213 return MaskPrefixLength(mask.bytes());
215 } 214 }
216 215
217 } // namespace net 216 } // namespace net
OLDNEW
« no previous file with comments | « net/base/host_port_pair.cc ('k') | net/base/parse_number.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698