Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix set_port to return correct boolean value #836

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions src/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

This is going to be slower. can we do it inside parse_port()

Copy link
Member Author

Choose a reason for hiding this comment

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

that was my initial attempt here e998da7, but these tests started failing:

6 - wpt_url_tests_typed.urltestdata_encoding<ada::url> (Failed)
9 - wpt_url_tests_typed.urltestdata_encoding<ada::url_aggregator> (Failed)
16 - url_components.urltestdata_encoding (Failed)

image

was my approach wrong?

if (parsed_port < 1) {
return false;
}

// Revert changes if parse_port fails.
std::optional<uint16_t> previous_port = port;
parse_port(trimmed);
Expand Down
6 changes: 6 additions & 0 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,11 @@ TYPED_TEST(basic_tests, negativeport) {
auto url = ada::parse<TypeParam>("https://www.google.com");
ASSERT_FALSE(url->set_port("-1"));
SUCCEED();
}
}

TYPED_TEST(basic_tests, negativeport_and_its_return) {
auto url = ada::parse<TypeParam>("fake://dummy.test");
ASSERT_FALSE(url->set_port("invalid80"));
ASSERT_TRUE(url->is_valid);
SUCCEED();
}
Loading