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

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

Issue 1560028: Adding extra argument DCHECKs to AddressList methods. (Closed)
Patch Set: Remove data_ check from head Created 10 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/address_list.h ('k') | 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/address_list.h" 5 #include "net/base/address_list.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "net/base/sys_addrinfo.h" 10 #include "net/base/sys_addrinfo.h"
11 11
12 namespace net { 12 namespace net {
13 13
14 namespace { 14 namespace {
15 15
16 // Make a copy of |info| (the dynamically-allocated parts are copied as well). 16 // Make a copy of |info| (the dynamically-allocated parts are copied as well).
17 // If |recursive| is true, chained entries via ai_next are copied too. 17 // If |recursive| is true, chained entries via ai_next are copied too.
18 // Copy returned by this function should be deleted using 18 // Copy returned by this function should be deleted using
19 // DeleteCopyOfAddrinfo(), and NOT freeaddrinfo(). 19 // DeleteCopyOfAddrinfo(), and NOT freeaddrinfo().
20 struct addrinfo* CreateCopyOfAddrinfo(const struct addrinfo* info, 20 struct addrinfo* CreateCopyOfAddrinfo(const struct addrinfo* info,
21 bool recursive) { 21 bool recursive) {
22 DCHECK(info);
22 struct addrinfo* copy = new addrinfo; 23 struct addrinfo* copy = new addrinfo;
23 24
24 // Copy all the fields (some of these are pointers, we will fix that next). 25 // Copy all the fields (some of these are pointers, we will fix that next).
25 memcpy(copy, info, sizeof(addrinfo)); 26 memcpy(copy, info, sizeof(addrinfo));
26 27
27 // ai_canonname is a NULL-terminated string. 28 // ai_canonname is a NULL-terminated string.
28 if (info->ai_canonname) { 29 if (info->ai_canonname) {
29 #ifdef OS_WIN 30 #ifdef OS_WIN
30 copy->ai_canonname = _strdup(info->ai_canonname); 31 copy->ai_canonname = _strdup(info->ai_canonname);
31 #else 32 #else
(...skipping 11 matching lines...) Expand all
43 if (recursive && info->ai_next) 44 if (recursive && info->ai_next)
44 copy->ai_next = CreateCopyOfAddrinfo(info->ai_next, recursive); 45 copy->ai_next = CreateCopyOfAddrinfo(info->ai_next, recursive);
45 else 46 else
46 copy->ai_next = NULL; 47 copy->ai_next = NULL;
47 48
48 return copy; 49 return copy;
49 } 50 }
50 51
51 // Free an addrinfo that was created by CreateCopyOfAddrinfo(). 52 // Free an addrinfo that was created by CreateCopyOfAddrinfo().
52 void FreeMyAddrinfo(struct addrinfo* info) { 53 void FreeMyAddrinfo(struct addrinfo* info) {
54 DCHECK(info);
53 if (info->ai_canonname) 55 if (info->ai_canonname)
54 free(info->ai_canonname); // Allocated by strdup. 56 free(info->ai_canonname); // Allocated by strdup.
55 57
56 if (info->ai_addr) 58 if (info->ai_addr)
57 delete [] reinterpret_cast<char*>(info->ai_addr); 59 delete [] reinterpret_cast<char*>(info->ai_addr);
58 60
59 struct addrinfo* next = info->ai_next; 61 struct addrinfo* next = info->ai_next;
60 62
61 delete info; 63 delete info;
62 64
63 // Recursive free. 65 // Recursive free.
64 if (next) 66 if (next)
65 FreeMyAddrinfo(next); 67 FreeMyAddrinfo(next);
66 } 68 }
67 69
68 // Returns the address to port field in |info|. 70 // Returns the address to port field in |info|.
69 uint16* GetPortField(const struct addrinfo* info) { 71 uint16* GetPortField(const struct addrinfo* info) {
72 DCHECK(info);
70 if (info->ai_family == AF_INET) { 73 if (info->ai_family == AF_INET) {
71 DCHECK_EQ(sizeof(sockaddr_in), info->ai_addrlen); 74 DCHECK_EQ(sizeof(sockaddr_in), info->ai_addrlen);
72 struct sockaddr_in* sockaddr = 75 struct sockaddr_in* sockaddr =
73 reinterpret_cast<struct sockaddr_in*>(info->ai_addr); 76 reinterpret_cast<struct sockaddr_in*>(info->ai_addr);
74 return &sockaddr->sin_port; 77 return &sockaddr->sin_port;
75 } else if (info->ai_family == AF_INET6) { 78 } else if (info->ai_family == AF_INET6) {
76 DCHECK_EQ(sizeof(sockaddr_in6), info->ai_addrlen); 79 DCHECK_EQ(sizeof(sockaddr_in6), info->ai_addrlen);
77 struct sockaddr_in6* sockaddr = 80 struct sockaddr_in6* sockaddr =
78 reinterpret_cast<struct sockaddr_in6*>(info->ai_addr); 81 reinterpret_cast<struct sockaddr_in6*>(info->ai_addr);
79 return &sockaddr->sin6_port; 82 return &sockaddr->sin6_port;
(...skipping 19 matching lines...) Expand all
99 void AddressList::Adopt(struct addrinfo* head) { 102 void AddressList::Adopt(struct addrinfo* head) {
100 data_ = new Data(head, true /*is_system_created*/); 103 data_ = new Data(head, true /*is_system_created*/);
101 } 104 }
102 105
103 void AddressList::Copy(const struct addrinfo* head, bool recursive) { 106 void AddressList::Copy(const struct addrinfo* head, bool recursive) {
104 data_ = new Data(CreateCopyOfAddrinfo(head, recursive), 107 data_ = new Data(CreateCopyOfAddrinfo(head, recursive),
105 false /*is_system_created*/); 108 false /*is_system_created*/);
106 } 109 }
107 110
108 void AddressList::Append(const struct addrinfo* head) { 111 void AddressList::Append(const struct addrinfo* head) {
112 DCHECK(head);
109 struct addrinfo* new_head; 113 struct addrinfo* new_head;
110 if (data_->is_system_created) { 114 if (data_->is_system_created) {
111 new_head = CreateCopyOfAddrinfo(data_->head, true); 115 new_head = CreateCopyOfAddrinfo(data_->head, true);
112 data_ = new Data(new_head, false /*is_system_created*/); 116 data_ = new Data(new_head, false /*is_system_created*/);
113 } else { 117 } else {
114 new_head = data_->head; 118 new_head = data_->head;
115 } 119 }
116 120
117 // Find the end of current linked list and append new data there. 121 // Find the end of current linked list and append new data there.
118 struct addrinfo* copy_ptr = new_head; 122 struct addrinfo* copy_ptr = new_head;
119 while (copy_ptr->ai_next) 123 while (copy_ptr->ai_next)
120 copy_ptr = copy_ptr->ai_next; 124 copy_ptr = copy_ptr->ai_next;
125 DCHECK(!head->ai_canonname);
121 copy_ptr->ai_next = CreateCopyOfAddrinfo(head, true); 126 copy_ptr->ai_next = CreateCopyOfAddrinfo(head, true);
122 } 127 }
123 128
124 void AddressList::SetPort(int port) { 129 void AddressList::SetPort(int port) {
125 SetPortRecursive(data_->head, port); 130 SetPortRecursive(data_->head, port);
126 } 131 }
127 132
128 int AddressList::GetPort() const { 133 int AddressList::GetPort() const {
129 uint16* port_field = GetPortField(data_->head); 134 uint16* port_field = GetPortField(data_->head);
130 if (!port_field) 135 if (!port_field)
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 new char[ai->ai_addrlen]); 174 new char[ai->ai_addrlen]);
170 memset(addr6, 0, sizeof(struct sockaddr_in6)); 175 memset(addr6, 0, sizeof(struct sockaddr_in6));
171 176
172 ai->ai_addr = reinterpret_cast<struct sockaddr*>(addr6); 177 ai->ai_addr = reinterpret_cast<struct sockaddr*>(addr6);
173 addr6->sin6_family = AF_INET6; 178 addr6->sin6_family = AF_INET6;
174 memcpy(&addr6->sin6_addr, data, 16); 179 memcpy(&addr6->sin6_addr, data, 16);
175 180
176 return AddressList(new Data(ai, false /*is_system_created*/)); 181 return AddressList(new Data(ai, false /*is_system_created*/));
177 } 182 }
178 183
184 AddressList::Data::Data(struct addrinfo* ai, bool is_system_created)
185 : head(ai), is_system_created(is_system_created) {
186 DCHECK(head);
187 }
188
179 AddressList::Data::~Data() { 189 AddressList::Data::~Data() {
180 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who 190 // Call either freeaddrinfo(head), or FreeMyAddrinfo(head), depending who
181 // created the data. 191 // created the data.
182 if (is_system_created) 192 if (is_system_created)
183 freeaddrinfo(head); 193 freeaddrinfo(head);
184 else 194 else
185 FreeMyAddrinfo(head); 195 FreeMyAddrinfo(head);
186 } 196 }
187 197
188 } // namespace net 198 } // namespace net
OLDNEW
« no previous file with comments | « net/base/address_list.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698