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

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

Issue 1692353002: Make IP Address related functions return the empty string when used on an invalid address. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments eroman Created 4 years, 10 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_number.h" 5 #include "net/base/ip_address_number.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 96
97 std::string IPAddressToString(const uint8_t* address, size_t address_len) { 97 std::string IPAddressToString(const uint8_t* address, size_t address_len) {
98 std::string str; 98 std::string str;
99 url::StdStringCanonOutput output(&str); 99 url::StdStringCanonOutput output(&str);
100 100
101 if (address_len == kIPv4AddressSize) { 101 if (address_len == kIPv4AddressSize) {
102 url::AppendIPv4Address(address, &output); 102 url::AppendIPv4Address(address, &output);
103 } else if (address_len == kIPv6AddressSize) { 103 } else if (address_len == kIPv6AddressSize) {
104 url::AppendIPv6Address(address, &output); 104 url::AppendIPv6Address(address, &output);
105 } else { 105 } else {
106 CHECK(false) << "Invalid IP address with length: " << address_len; 106 return std::string();
107 } 107 }
108 108
109 output.Complete(); 109 output.Complete();
110 return str; 110 return str;
111 } 111 }
112 112
113 std::string IPAddressToStringWithPort(const uint8_t* address, 113 std::string IPAddressToStringWithPort(const uint8_t* address,
114 size_t address_len, 114 size_t address_len,
115 uint16_t port) { 115 uint16_t port) {
116 std::string address_str = IPAddressToString(address, address_len); 116 std::string address_str = IPAddressToString(address, address_len);
117 if (address_str.empty())
118 return address_str;
117 119
118 if (address_len == kIPv6AddressSize) { 120 if (address_len == kIPv6AddressSize) {
119 // Need to bracket IPv6 addresses since they contain colons. 121 // Need to bracket IPv6 addresses since they contain colons.
120 return base::StringPrintf("[%s]:%d", address_str.c_str(), port); 122 return base::StringPrintf("[%s]:%d", address_str.c_str(), port);
121 } 123 }
122 return base::StringPrintf("%s:%d", address_str.c_str(), port); 124 return base::StringPrintf("%s:%d", address_str.c_str(), port);
123 } 125 }
124 126
125 std::string IPAddressToString(const IPAddressNumber& addr) { 127 std::string IPAddressToString(const IPAddressNumber& addr) {
126 return IPAddressToString(&addr.front(), addr.size()); 128 return IPAddressToString(&addr.front(), addr.size());
eroman 2016/02/17 16:11:36 Change &addr.front() to addr.data()
martijnc 2016/02/17 21:46:55 Done.
127 } 129 }
128 130
129 std::string IPAddressToStringWithPort(const IPAddressNumber& addr, 131 std::string IPAddressToStringWithPort(const IPAddressNumber& addr,
130 uint16_t port) { 132 uint16_t port) {
131 return IPAddressToStringWithPort(&addr.front(), addr.size(), port); 133 return IPAddressToStringWithPort(&addr.front(), addr.size(), port);
eroman 2016/02/17 16:11:36 Same thing here: Change &addr.front() to addr.data
martijnc 2016/02/17 21:46:55 Done.
132 } 134 }
133 135
134 std::string IPAddressToPackedString(const IPAddressNumber& addr) { 136 std::string IPAddressToPackedString(const IPAddressNumber& addr) {
135 return std::string(reinterpret_cast<const char *>(&addr.front()), 137 return std::string(reinterpret_cast<const char *>(&addr.front()),
eroman 2016/02/17 16:11:36 May as well make the change here too, although thi
martijnc 2016/02/17 21:46:55 Done.
136 addr.size()); 138 addr.size());
137 } 139 }
138 140
139 bool ParseURLHostnameToNumber(const std::string& hostname, 141 bool ParseURLHostnameToNumber(const std::string& hostname,
140 IPAddressNumber* ip_number) { 142 IPAddressNumber* ip_number) {
141 // |hostname| is an already canoncalized hostname, conforming to RFC 3986. 143 // |hostname| is an already canoncalized hostname, conforming to RFC 3986.
142 // For an IP address, this is defined in Section 3.2.2 of RFC 3986, with 144 // For an IP address, this is defined in Section 3.2.2 of RFC 3986, with
143 // the canonical form for IPv6 addresses defined in Section 4 of RFC 5952. 145 // the canonical form for IPv6 addresses defined in Section 4 of RFC 5952.
144 url::Component host_comp(0, hostname.size()); 146 url::Component host_comp(0, hostname.size());
145 147
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 } 292 }
291 return a1.size() * CHAR_BIT; 293 return a1.size() * CHAR_BIT;
292 } 294 }
293 295
294 unsigned MaskPrefixLength(const IPAddressNumber& mask) { 296 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
295 IPAddressNumber all_ones(mask.size(), 0xFF); 297 IPAddressNumber all_ones(mask.size(), 0xFF);
296 return CommonPrefixLength(mask, all_ones); 298 return CommonPrefixLength(mask, all_ones);
297 } 299 }
298 300
299 } // namespace net 301 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698