diff --git a/src/url.cpp b/src/url.cpp index f35625bed..f7fa85197 100644 --- a/src/url.cpp +++ b/src/url.cpp @@ -782,12 +782,9 @@ bool url::set_port(const std::string_view input) { port = std::nullopt; return true; } - // Input should not start with control characters. - if (ada::unicode::is_c0_control_or_space(trimmed.front())) { - return false; - } - // Input should contain at least one ascii digit. - if (input.find_first_of("0123456789") == std::string_view::npos) { + + // Input should not start with a non-digit character. + if (!ada::unicode::is_ascii_digit(trimmed.front())) { return false; } diff --git a/src/url_aggregator.cpp b/src/url_aggregator.cpp index 08211436d..b6151013a 100644 --- a/src/url_aggregator.cpp +++ b/src/url_aggregator.cpp @@ -285,12 +285,9 @@ bool url_aggregator::set_port(const std::string_view input) { clear_port(); return true; } - // Input should not start with control characters. - if (ada::unicode::is_c0_control_or_space(trimmed.front())) { - return false; - } - // Input should contain at least one ascii digit. - if (input.find_first_of("0123456789") == std::string_view::npos) { + + // Input should not start with a non-digit character. + if (!ada::unicode::is_ascii_digit(trimmed.front())) { return false; } diff --git a/tests/basic_tests.cpp b/tests/basic_tests.cpp index d1d452a42..33dc7f966 100644 --- a/tests/basic_tests.cpp +++ b/tests/basic_tests.cpp @@ -462,4 +462,17 @@ 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 +} + +// https://github.com/ada-url/ada/issues/826 +TYPED_TEST(basic_tests, set_invalid_port) { + auto url = ada::parse("fake://dummy.test"); + ASSERT_TRUE(url); + ASSERT_FALSE(url->set_port("invalid80")); + ASSERT_EQ(url->get_port(), ""); + ASSERT_TRUE(url->set_port("80valid")); + ASSERT_TRUE(url->is_valid); + ASSERT_EQ(url->get_port(), "80"); + ASSERT_TRUE(url->is_valid); + SUCCEED(); +}