Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 43956e9

Browse filesBrowse 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 9c12582 commit 43956e9
Copy full SHA for 43956e9

File tree

Expand file treeCollapse file tree

1 file changed

+13
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+13
-6
lines changed
Open diff view settings
Collapse file

‎src/node_url.cc‎

Copy file name to clipboardExpand all lines: src/node_url.cc
+13-6Lines changed: 13 additions & 6 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.