Skip to content

Commit

Permalink
src: free memory before re-setting URLHost value
Browse files Browse the repository at this point in the history
Fixes: #18302

PR-URL: #18357
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
  • Loading branch information
prog1dev authored and MylesBorins committed Mar 20, 2018
1 parent b2e8cce commit 265faf8
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class URLHost {
Value value_;
HostType type_ = HostType::H_FAILED;

inline void Reset() {
using string = std::string;
switch (type_) {
case HostType::H_DOMAIN: value_.domain.~string(); break;
case HostType::H_OPAQUE: value_.opaque.~string(); break;
default: break;
}
type_ = HostType::H_FAILED;
}

// Setting the string members of the union with = is brittle because
// it relies on them being initialized to a state that requires no
// destruction of old data.
Expand All @@ -101,23 +111,20 @@ class URLHost {
// These helpers are the easiest solution but we might want to consider
// just not forcing strings into an union.
inline void SetOpaque(std::string&& string) {
Reset();
type_ = HostType::H_OPAQUE;
new(&value_.opaque) std::string(std::move(string));
}

inline void SetDomain(std::string&& string) {
Reset();
type_ = HostType::H_DOMAIN;
new(&value_.domain) std::string(std::move(string));
}
};

URLHost::~URLHost() {
using string = std::string;
switch (type_) {
case HostType::H_DOMAIN: value_.domain.~string(); break;
case HostType::H_OPAQUE: value_.opaque.~string(); break;
default: break;
}
Reset();
}

#define ARGS(XX) \
Expand Down

0 comments on commit 265faf8

Please sign in to comment.