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

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

Issue 919023003: DNSDomainFromDot: Reject empty labels. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 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 | « no previous file | net/base/dns_util_unittest.cc » ('j') | net/dns/host_resolver_proc.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/dns_util.h" 5 #include "net/base/dns_util.h"
6 6
7 #include <cstring> 7 #include <cstring>
8 8
9 namespace net { 9 namespace net {
10 10
11 // Based on DJB's public domain code. 11 // Based on DJB's public domain code.
12 bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) { 12 bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) {
13 const char* buf = dotted.data(); 13 const char* buf = dotted.data();
14 unsigned n = dotted.size(); 14 unsigned n = dotted.size();
15 char label[63]; 15 char label[63];
16 size_t labellen = 0; /* <= sizeof label */ 16 size_t labellen = 0; /* <= sizeof label */
17 char name[255]; 17 char name[255];
18 size_t namelen = 0; /* <= sizeof name */ 18 size_t namelen = 0; /* <= sizeof name */
19 char ch; 19 char ch;
20 20
21 for (;;) { 21 for (;;) {
22 if (!n) 22 if (!n)
23 break; 23 break;
24 ch = *buf++; 24 ch = *buf++;
25 --n; 25 --n;
26 if (ch == '.') { 26 if (ch == '.') {
27 if (labellen) { 27 // Don't allow empty labels per http://crbug.com/456391.
28 if (namelen + labellen + 1 > sizeof name) 28 if (!labellen)
29 return false; 29 return false;
30 name[namelen++] = static_cast<char>(labellen); 30 if (namelen + labellen + 1 > sizeof name)
31 memcpy(name + namelen, label, labellen); 31 return false;
32 namelen += labellen; 32 name[namelen++] = static_cast<char>(labellen);
33 labellen = 0; 33 memcpy(name + namelen, label, labellen);
34 } 34 namelen += labellen;
35 labellen = 0;
35 continue; 36 continue;
36 } 37 }
37 if (labellen >= sizeof label) 38 if (labellen >= sizeof label)
38 return false; 39 return false;
39 label[labellen++] = ch; 40 label[labellen++] = ch;
40 } 41 }
41 42
43 // Allow empty label at end of name to disable suffix search.
42 if (labellen) { 44 if (labellen) {
43 if (namelen + labellen + 1 > sizeof name) 45 if (namelen + labellen + 1 > sizeof name)
44 return false; 46 return false;
45 name[namelen++] = static_cast<char>(labellen); 47 name[namelen++] = static_cast<char>(labellen);
46 memcpy(name + namelen, label, labellen); 48 memcpy(name + namelen, label, labellen);
47 namelen += labellen; 49 namelen += labellen;
48 labellen = 0; 50 labellen = 0;
49 } 51 }
50 52
51 if (namelen + 1 > sizeof name) 53 if (namelen + 1 > sizeof name)
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 std::string TrimEndingDot(const base::StringPiece& host) { 85 std::string TrimEndingDot(const base::StringPiece& host) {
84 base::StringPiece host_trimmed = host; 86 base::StringPiece host_trimmed = host;
85 size_t len = host_trimmed.length(); 87 size_t len = host_trimmed.length();
86 if (len > 1 && host_trimmed[len - 1] == '.') { 88 if (len > 1 && host_trimmed[len - 1] == '.') {
87 host_trimmed.remove_suffix(1); 89 host_trimmed.remove_suffix(1);
88 } 90 }
89 return host_trimmed.as_string(); 91 return host_trimmed.as_string();
90 } 92 }
91 93
92 } // namespace net 94 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/base/dns_util_unittest.cc » ('j') | net/dns/host_resolver_proc.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698