diff --git a/iocore/net/P_SSLSNI.h b/iocore/net/P_SSLSNI.h index 43055eb467c..33d753e67c7 100644 --- a/iocore/net/P_SSLSNI.h +++ b/iocore/net/P_SSLSNI.h @@ -32,6 +32,7 @@ #include #include +#include #include "ProxyConfig.h" #include "P_SNIActionPerformer.h" @@ -50,10 +51,28 @@ struct NextHopProperty { using actionVector = std::vector>; +struct pcreFreer { + void + operator()(void *p) + { + pcre_free(p); + } +}; + struct namedElement { public: namedElement() {} + namedElement & + operator=(namedElement &&other) + { + if (this != &other) { + match = std::move(other.match); + } + return *this; + } + namedElement(namedElement &&other) { *this = std::move(other); } + void setGlobName(std::string name) { @@ -76,13 +95,11 @@ struct namedElement { const char *err_ptr; int err_offset = 0; if (!regexName.empty()) { - match = pcre_compile(regexName.c_str(), PCRE_ANCHORED, &err_ptr, &err_offset, nullptr); - } else { - match = nullptr; + match.reset(pcre_compile(regexName.c_str(), PCRE_ANCHORED, &err_ptr, &err_offset, nullptr)); } } - pcre *match = nullptr; + std::unique_ptr match; }; struct actionElement : public namedElement { diff --git a/iocore/net/SSLSNIConfig.cc b/iocore/net/SSLSNIConfig.cc index c0f2c5eca5f..93a8243fcc9 100644 --- a/iocore/net/SSLSNIConfig.cc +++ b/iocore/net/SSLSNIConfig.cc @@ -45,7 +45,7 @@ SNIConfigParams::getPropertyConfig(const std::string &servername) const { const NextHopProperty *nps = nullptr; for (auto &&item : next_hop_list) { - if (pcre_exec(item.match, nullptr, servername.c_str(), servername.length(), 0, 0, nullptr, 0) >= 0) { + if (pcre_exec(item.match.get(), nullptr, servername.c_str(), servername.length(), 0, 0, nullptr, 0) >= 0) { // Found a match nps = &item.prop; break; @@ -118,7 +118,8 @@ SNIConfigParams::get(const std::string &servername) const int length = servername.length(); if (retval.match == nullptr && length == 0) { return {&retval.actions, context}; - } else if (auto offset = pcre_exec(retval.match, nullptr, servername.c_str(), length, 0, 0, ovector, OVECSIZE); offset >= 0) { + } else if (auto offset = pcre_exec(retval.match.get(), nullptr, servername.c_str(), length, 0, 0, ovector, OVECSIZE); + offset >= 0) { if (offset == 1) { // first pair identify the portion of the subject string matched by the entire pattern if (ovector[0] == 0 && ovector[1] == length) {