Skip to content

Commit 6be6c24

Browse files
authored
Applied fix for #116 (#117)
1 parent 3c223b7 commit 6be6c24

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

include/network/uri/detail/uri_parts.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ struct hierarchical_part {
5858
optional<uri_part> host;
5959
optional<uri_part> port;
6060
optional<uri_part> path;
61+
62+
void clear() {
63+
user_info = nullopt;
64+
host = nullopt;
65+
port = nullopt;
66+
path = nullopt;
67+
}
6168
};
6269

6370
struct uri_parts {
@@ -67,6 +74,13 @@ struct uri_parts {
6774
hierarchical_part hier_part;
6875
optional<uri_part> query;
6976
optional<uri_part> fragment;
77+
78+
void clear() {
79+
scheme = nullopt;
80+
hier_part.clear();
81+
query = nullopt;
82+
fragment = nullopt;
83+
}
7084
};
7185
} // namespace detail
7286
} // namespace network

src/uri.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ void uri::swap(uri &other) noexcept {
196196
other.uri_view_ = other.uri_;
197197

198198
const auto this_parts = uri_parts_;
199+
uri_parts_.clear();
199200
detail::advance_parts(uri_view_, uri_parts_, other.uri_parts_);
201+
other.uri_parts_.clear();
200202
detail::advance_parts(other.uri_view_, other.uri_parts_, this_parts);
201203
}
202204

test/uri_builder_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,3 +775,26 @@ TEST(builder_test, scheme_and_absolute_path) {
775775
ASSERT_EQ("foo", builder.uri().scheme());
776776
ASSERT_EQ("/bar", builder.uri().path());
777777
}
778+
779+
TEST(builder_test, assignment_operator_bug_116) {
780+
// https://github.com/cpp-netlib/uri/issues/116
781+
network::uri a("http://a.com:1234");
782+
ASSERT_TRUE(a.has_port());
783+
784+
const network::uri b("http://b.com");
785+
ASSERT_FALSE(b.has_port());
786+
787+
a = b;
788+
ASSERT_FALSE(a.has_port()) << a.string();
789+
}
790+
791+
TEST(builder_test, construct_from_uri_bug_116) {
792+
// https://github.com/cpp-netlib/uri/issues/116
793+
network::uri a("http://a.com:1234");
794+
const network::uri b("http://b.com");
795+
a = b;
796+
797+
network::uri_builder ub(a); // ASAN reports heap-use-after-free here
798+
const network::uri c(ub.uri());
799+
ASSERT_FALSE(c.has_port()) << c.string();
800+
}

test/uri_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,3 +1058,14 @@ TEST(uri_test, uri_has_host_bug_88_2) {
10581058

10591059
EXPECT_EQ("example.com", instance.host().to_string());
10601060
}
1061+
1062+
TEST(uri_test, assignment_operator_bug_116) {
1063+
network::uri a("http://a.com:1234");
1064+
ASSERT_TRUE(a.has_port());
1065+
1066+
const network::uri b("http://b.com");
1067+
ASSERT_FALSE(b.has_port());
1068+
1069+
a = b;
1070+
ASSERT_FALSE(a.has_port()) << a.string() << ", " << a.port();
1071+
}

0 commit comments

Comments
 (0)