Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions ext/include/opentelemetry/ext/http/common/url_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

#pragma once

#include <stdlib.h>
#include <cerrno>
#include <cstdint>
#include <cstdlib>
#include <string>

#include "opentelemetry/version.h"
Expand Down Expand Up @@ -88,8 +89,11 @@ class UrlParser
path_ = std::string("/"); // use default path
if (is_port)
{
port_ = static_cast<uint16_t>(
std::stoi(std::string(url_.begin() + cpos, url_.begin() + url_.length())));
std::string port_str(
url_.begin() + static_cast<std::string::difference_type>(cpos),
url_.begin() + static_cast<std::string::difference_type>(url_.length()));
Comment on lines +93 to +94
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static_cast<> is because "Narrowing conversion from 'size_t' (aka 'unsigned long') to signed type 'difference_type' (aka 'long') is implementation-defined"

See also: https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.html


port_ = GetPort(port_str);
}
else
{
Expand All @@ -99,8 +103,9 @@ class UrlParser
}
if (is_port)
{
port_ =
static_cast<uint16_t>(std::stoi(std::string(url_.begin() + cpos, url_.begin() + pos)));
std::string port_str(url_.begin() + static_cast<std::string::difference_type>(cpos),
url_.begin() + static_cast<std::string::difference_type>(pos));
port_ = GetPort(port_str);
}
else
{
Expand Down Expand Up @@ -164,6 +169,20 @@ class UrlParser

return std::string::npos;
}

std::uint16_t GetPort(const std::string &s)
{
char *e = nullptr;
errno = 0;
auto port = std::strtol(s.c_str(), &e, 10);
if (e == s.c_str() || e != s.c_str() + s.size() || errno == ERANGE || port < 0 || port > 65535)
{
success_ = false;
return 0;
}

return static_cast<uint16_t>(port);
}
};

class UrlDecoder
Expand Down
35 changes: 35 additions & 0 deletions ext/test/http/url_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,41 @@ TEST(UrlParserTests, BasicTests)
{"path", "/path1@bbb/path2"},
{"query", "q1=a1&q2=a2"},
{"success", "true"}}},
{"https://https://example.com/some/path",
{{"host", "https"},
{"port", "0"},
{"scheme", "https"},
{"path", "//example.com/some/path"},
{"query", ""},
{"success", "false"}}},
{"https://example.com:-1/some/path",
{{"host", "example.com"},
{"port", "0"},
{"scheme", "https"},
{"path", "/some/path"},
{"query", ""},
{"success", "false"}}},
{"https://example.com:65536/some/path",
{{"host", "example.com"},
{"port", "0"},
{"scheme", "https"},
{"path", "/some/path"},
{"query", ""},
{"success", "false"}}},
{"https://example.com:80a/some/path",
{{"host", "example.com"},
{"port", "0"},
{"scheme", "https"},
{"path", "/some/path"},
{"query", ""},
{"success", "false"}}},
{"https://example.com:18446744073709551616/some/path",
{{"host", "example.com"},
{"port", "0"},
{"scheme", "https"},
{"path", "/some/path"},
{"query", ""},
{"success", "false"}}},
};
for (auto &url_map : urls_map)
{
Expand Down