Skip to content

Commit c0f40be

Browse files
prog1devMylesBorins
authored andcommitted
src: free memory before re-setting URLHost value
Fixes: #18302 PR-URL: #18357 Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent a1fb263 commit c0f40be

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/node_url.cc

+13-6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ class URLHost {
9292
Value value_;
9393
HostType type_ = HostType::H_FAILED;
9494

95+
inline void Reset() {
96+
using string = std::string;
97+
switch (type_) {
98+
case HostType::H_DOMAIN: value_.domain.~string(); break;
99+
case HostType::H_OPAQUE: value_.opaque.~string(); break;
100+
default: break;
101+
}
102+
type_ = HostType::H_FAILED;
103+
}
104+
95105
// Setting the string members of the union with = is brittle because
96106
// it relies on them being initialized to a state that requires no
97107
// destruction of old data.
@@ -101,23 +111,20 @@ class URLHost {
101111
// These helpers are the easiest solution but we might want to consider
102112
// just not forcing strings into an union.
103113
inline void SetOpaque(std::string&& string) {
114+
Reset();
104115
type_ = HostType::H_OPAQUE;
105116
new(&value_.opaque) std::string(std::move(string));
106117
}
107118

108119
inline void SetDomain(std::string&& string) {
120+
Reset();
109121
type_ = HostType::H_DOMAIN;
110122
new(&value_.domain) std::string(std::move(string));
111123
}
112124
};
113125

114126
URLHost::~URLHost() {
115-
using string = std::string;
116-
switch (type_) {
117-
case HostType::H_DOMAIN: value_.domain.~string(); break;
118-
case HostType::H_OPAQUE: value_.opaque.~string(); break;
119-
default: break;
120-
}
127+
Reset();
121128
}
122129

123130
#define ARGS(XX) \

0 commit comments

Comments
 (0)