From bd831b42fc95cae0bef8cc2e7d9d38f084503901 Mon Sep 17 00:00:00 2001 From: Ivan Filenko Date: Wed, 24 Jan 2018 01:42:40 +0300 Subject: [PATCH 1/3] src: define URLHost::Reset() function Fixes: https://github.com/nodejs/node/issues/18302 --- src/node_url.cc | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/node_url.cc b/src/node_url.cc index 853a23d40d7d1e..a5ed89fa84eb78 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -92,6 +92,15 @@ 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; + } + } + // 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. @@ -112,12 +121,7 @@ class URLHost { }; 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) \ From 4a2adb01b357601c5903917723f7d3603c63327e Mon Sep 17 00:00:00 2001 From: Ivan Filenko Date: Wed, 24 Jan 2018 01:45:00 +0300 Subject: [PATCH 2/3] src: free memory first on URLHost::SetOpaque & URLHost::SetDomain call Fixes: https://github.com/nodejs/node/issues/18302 --- src/node_url.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/node_url.cc b/src/node_url.cc index a5ed89fa84eb78..6a96a9e3fa2bea 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -110,11 +110,13 @@ 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)); } From a7c310fe8dc3f808571b56621dd8432dc130bd57 Mon Sep 17 00:00:00 2001 From: Ivan Filenko Date: Wed, 24 Jan 2018 23:22:25 +0300 Subject: [PATCH 3/3] src: reset _type on URLHost::Reset() call --- src/node_url.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/node_url.cc b/src/node_url.cc index 6a96a9e3fa2bea..cac2831af6ef7f 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -99,6 +99,7 @@ class URLHost { 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