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

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: 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
« no previous file with comments | « net/base/ip_address_number.h ('k') | net/base/ip_address_number_unittest.cc » ('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) 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 if (address_len != kIPv4AddressSize && address_len != kIPv6AddressSize)
eroman 2016/02/16 19:48:52 How about instead, put the following line after IP
martijnc 2016/02/16 22:02:00 Done.
117 return std::string();
118
116 std::string address_str = IPAddressToString(address, address_len); 119 std::string address_str = IPAddressToString(address, address_len);
117 120
118 if (address_len == kIPv6AddressSize) { 121 if (address_len == kIPv6AddressSize) {
119 // Need to bracket IPv6 addresses since they contain colons. 122 // Need to bracket IPv6 addresses since they contain colons.
120 return base::StringPrintf("[%s]:%d", address_str.c_str(), port); 123 return base::StringPrintf("[%s]:%d", address_str.c_str(), port);
121 } 124 }
122 return base::StringPrintf("%s:%d", address_str.c_str(), port); 125 return base::StringPrintf("%s:%d", address_str.c_str(), port);
123 } 126 }
124 127
125 std::string IPAddressToString(const IPAddressNumber& addr) { 128 std::string IPAddressToString(const IPAddressNumber& addr) {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 } 293 }
291 return a1.size() * CHAR_BIT; 294 return a1.size() * CHAR_BIT;
292 } 295 }
293 296
294 unsigned MaskPrefixLength(const IPAddressNumber& mask) { 297 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
295 IPAddressNumber all_ones(mask.size(), 0xFF); 298 IPAddressNumber all_ones(mask.size(), 0xFF);
296 return CommonPrefixLength(mask, all_ones); 299 return CommonPrefixLength(mask, all_ones);
297 } 300 }
298 301
299 } // namespace net 302 } // namespace net
OLDNEW
« no previous file with comments | « net/base/ip_address_number.h ('k') | net/base/ip_address_number_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698