Skip to content

Commit e59a4b1

Browse files
committed
fix: address code review comments
1 parent c8749e3 commit e59a4b1

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

ext/include/opentelemetry/ext/http/common/url_parser.h

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ class UrlParser
9494
path_ = std::string("/"); // use default path
9595
if (is_port)
9696
{
97-
auto port_str = url_.substr(cpos, url_.length());
97+
auto port_str = url_.substr(cpos);
9898
port_ = GetPort(port_str);
9999
}
100100
else
101101
{
102-
host_ = url_.substr(cpos, url_.length());
102+
host_ = url_.substr(cpos);
103103
}
104104
return;
105105
}
@@ -119,21 +119,20 @@ class UrlParser
119119
pos = url_.find('?', cpos);
120120
if (pos == std::string::npos)
121121
{
122-
path_ = url_.substr(cpos, url_.length());
123-
query_ = "";
122+
path_ = url_.substr(cpos);
124123
}
125124
else
126125
{
127126
path_ = url_.substr(cpos, pos - cpos);
128127
cpos = pos + 1;
129-
query_ = url_.substr(cpos, url_.length());
128+
query_ = url_.substr(cpos);
130129
}
131130
return;
132131
}
133132
path_ = std::string("/");
134133
if (url_[cpos] == '?')
135134
{
136-
query_ = url_.substr(cpos, url_.length());
135+
query_ = url_.substr(cpos);
137136
}
138137
}
139138

@@ -207,16 +206,23 @@ class UrlDecoder
207206
for (size_t pos = 0; pos < encoded.size(); pos++)
208207
{
209208
auto c = encoded[pos];
210-
if (c == '%' && pos + 2 < encoded.size())
209+
if (c == '%')
211210
{
211+
if (pos + 2 >= encoded.size())
212+
{
213+
return encoded;
214+
}
215+
212216
int hi = hex_to_int(encoded[pos + 1]);
213217
int lo = hex_to_int(encoded[pos + 2]);
214218

215-
if (hi != -1 && lo != -1)
219+
if (hi == -1 || lo == -1)
216220
{
217-
c = static_cast<char>((hi << 4) | lo);
218-
pos += 2;
221+
return encoded;
219222
}
223+
224+
c = static_cast<char>((hi << 4) | lo);
225+
pos += 2;
220226
}
221227

222228
result.push_back(c);

ext/test/http/url_parser_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ TEST(UrlDecoderTests, BasicTests)
238238
{"%2x", "%2x"},
239239
{"%20", " "},
240240
{"text%2", "text%2"},
241-
};
241+
{"%20test%zztest", "%20test%zztest"},
242+
{"%20test%2", "%20test%2"}};
242243

243244
for (auto &testsample : testdata)
244245
{

0 commit comments

Comments
 (0)