Skip to content

Commit af2c41e

Browse files
committed
src: free memory before re-setting URLHost value
Fixes: nodejs#18302 PR-URL: nodejs#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 988cca8 commit af2c41e

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,25 +111,22 @@ 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();
106117
value_.opaque.swap(*string);
107118
}
108119

109120
inline void SetDomain(std::string* string) {
121+
Reset();
110122
type_ = HostType::H_DOMAIN;
111123
new(&value_.domain) std::string();
112124
value_.domain.swap(*string);
113125
}
114126
};
115127

116128
URLHost::~URLHost() {
117-
using string = std::string;
118-
switch (type_) {
119-
case HostType::H_DOMAIN: value_.domain.~string(); break;
120-
case HostType::H_OPAQUE: value_.opaque.~string(); break;
121-
default: break;
122-
}
129+
Reset();
123130
}
124131

125132
#define ARGS(XX) \

0 commit comments

Comments
 (0)