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

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

Issue 1829873002: Add base/parse_number.h to generalize number parsing done in //net. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more tests Created 4 years, 9 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_unittest.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_number_conversions.h"
8 #include "base/strings/string_piece.h" 7 #include "base/strings/string_piece.h"
9 #include "base/strings/string_split.h" 8 #include "base/strings/string_split.h"
10 #include "net/base/ip_address_number.h" 9 #include "net/base/ip_address_number.h"
10 #include "net/base/parse_number.h"
11 #include "url/gurl.h" 11 #include "url/gurl.h"
12 #include "url/url_canon_ip.h" 12 #include "url/url_canon_ip.h"
13 13
14 namespace net { 14 namespace net {
15 15
16 IPAddress::IPAddress() {} 16 IPAddress::IPAddress() {}
17 17
18 IPAddress::IPAddress(const IPAddressNumber& address) : ip_address_(address) {} 18 IPAddress::IPAddress(const IPAddressNumber& address) : ip_address_(address) {}
19 19
20 IPAddress::IPAddress(const IPAddress& other) = default; 20 IPAddress::IPAddress(const IPAddress& other) = default;
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 172
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 // TODO(martijnc): Find a more general solution for the overly permissive
183 // base::StringToInt() parsing. https://crbug.com/596523.
184 const base::StringPiece& prefix_length = parts[1];
185 if (prefix_length.starts_with("+"))
186 return false;
187
188 // Parse the prefix length. 182 // Parse the prefix length.
189 int number_of_bits = -1; 183 int number_of_bits = -1;
190 if (!base::StringToInt(prefix_length, &number_of_bits)) 184 if (!ParseNonNegativeDecimalInt(parts[1], &number_of_bits))
191 return false; 185 return false;
192 186
193 // Make sure the prefix length is in a valid range. 187 // Make sure the prefix length is in a valid range.
194 if (number_of_bits < 0 || 188 if (number_of_bits < 0 ||
195 number_of_bits > static_cast<int>(ip_address->size() * 8)) 189 number_of_bits > static_cast<int>(ip_address->size() * 8))
196 return false; 190 return false;
197 191
198 *prefix_length_in_bits = static_cast<size_t>(number_of_bits); 192 *prefix_length_in_bits = static_cast<size_t>(number_of_bits);
199 return true; 193 return true;
200 } 194 }
201 195
202 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) { 196 unsigned CommonPrefixLength(const IPAddress& a1, const IPAddress& a2) {
203 return CommonPrefixLength(a1.bytes(), a2.bytes()); 197 return CommonPrefixLength(a1.bytes(), a2.bytes());
204 } 198 }
205 199
206 unsigned MaskPrefixLength(const IPAddress& mask) { 200 unsigned MaskPrefixLength(const IPAddress& mask) {
207 return MaskPrefixLength(mask.bytes()); 201 return MaskPrefixLength(mask.bytes());
208 } 202 }
209 203
210 } // namespace net 204 } // namespace net
OLDNEW
« no previous file with comments | « net/base/host_port_pair_unittest.cc ('k') | net/base/parse_number.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698