diff --git a/src/url.cpp b/src/url.cpp index f35625bed..29416d8df 100644 --- a/src/url.cpp +++ b/src/url.cpp @@ -791,6 +791,12 @@ bool url::set_port(const std::string_view input) { return false; } + uint16_t parsed_port{}; + std::from_chars(trimmed.data(), trimmed.data() + trimmed.size(), parsed_port); + if (parsed_port < 1) { + return false; + } + // Revert changes if parse_port fails. std::optional previous_port = port; parse_port(trimmed); diff --git a/src/url_aggregator.cpp b/src/url_aggregator.cpp index 08211436d..f1b26b528 100644 --- a/src/url_aggregator.cpp +++ b/src/url_aggregator.cpp @@ -294,6 +294,12 @@ bool url_aggregator::set_port(const std::string_view input) { return false; } + uint16_t parsed_port{}; + std::from_chars(trimmed.data(), trimmed.data() + trimmed.size(), parsed_port); + if (parsed_port < 1) { + return false; + } + // Revert changes if parse_port fails. uint32_t previous_port = components.port; parse_port(trimmed); diff --git a/tests/basic_tests.cpp b/tests/basic_tests.cpp index d1d452a42..255b96ab1 100644 --- a/tests/basic_tests.cpp +++ b/tests/basic_tests.cpp @@ -462,4 +462,11 @@ TYPED_TEST(basic_tests, negativeport) { auto url = ada::parse("https://www.google.com"); ASSERT_FALSE(url->set_port("-1")); SUCCEED(); -} \ No newline at end of file +} + +TYPED_TEST(basic_tests, negativeport_and_its_return) { + auto url = ada::parse("fake://dummy.test"); + ASSERT_FALSE(url->set_port("invalid80")); + ASSERT_TRUE(url->is_valid); + SUCCEED(); +}