1
0
Fork 0
mirror of https://bitbucket.org/King_DuckZ/keepupnpup.git synced 2024-11-07 21:29:00 +00:00

Crash and resource leak fix

This commit is contained in:
King_DuckZ 2016-08-26 03:46:33 +02:00
parent 88d891a448
commit e0d6cd106b

View file

@ -24,6 +24,7 @@
#include <sstream>
#include <ciso646>
#include <strings.h>
#include <cassert>
#if !defined(NDEBUG)
# define KUU_VERBOSE
@ -51,11 +52,13 @@ namespace kuu {
struct UPNPUrlsWithInitFlag {
UPNPUrlsWithInitFlag() : initialized(false) {}
explicit UPNPUrlsWithInitFlag(std::nullptr_t) : initialized(false) {}
explicit UPNPUrlsWithInitFlag(bool parInit) : initialized(parInit) {}
struct UPNPUrls urls;
bool initialized;
operator struct UPNPUrls* () { return &urls; }
UPNPUrlsWithInitFlag* operator-> () { return this; }
operator struct UPNPUrls* () { return (initialized ? &urls : nullptr); }
UPNPUrls* operator-> () { return (initialized ? &urls : nullptr); }
bool operator not() const { return not initialized; }
};
@ -179,15 +182,19 @@ namespace kuu {
throw ScanException("More than one UPNP device found (" + std::to_string(devs.size()) + ")");
UPNPUrlsPtr urls;
assert(not urls.get());
{
UPNPUrlsWithInitFlag urls_local(true);
assert(urls_local.initialized);
std::array<char, 64> lanaddr;
const auto i = UPNP_GetValidIGD(
devlist.get(),
urls.get(),
urls_local,
&m_local_data->data,
lanaddr.data(),
lanaddr.size()
);
urls.reset(urls_local);
switch (i) {
case 1:
@ -198,14 +205,13 @@ namespace kuu {
default:
m_local_data->igd_reply = IGDNotSure;
}
urls->initialized = true;
m_local_data->lanaddr = lanaddr.data();
}
{
std::array<char, 64> externaladdr;
const auto r = UPNP_GetExternalIPAddress(urls->urls.controlURL, m_local_data->data.first.servicetype, externaladdr.data());
const auto r = UPNP_GetExternalIPAddress(urls->controlURL, m_local_data->data.first.servicetype, externaladdr.data());
if (UPNPCOMMAND_SUCCESS != r)
m_local_data->externaladdr = externaladdr.data();
}
@ -254,7 +260,7 @@ namespace kuu {
intClient[0] = '\0';
r = UPNP_GetGenericPortMappingEntry(
m_local_data->urls->urls.controlURL,
m_local_data->urls->controlURL,
m_local_data->data.first.servicetype,
std::to_string(z).c_str(),
extPort,
@ -299,7 +305,7 @@ namespace kuu {
Protocol parProtocol
) {
const auto r = UPNP_AddPortMapping(
m_local_data->urls->urls.controlURL,
m_local_data->urls->controlURL,
m_local_data->data.first.servicetype,
std::to_string(parExtPort).c_str(),
std::to_string(parIntPort).c_str(),
@ -312,7 +318,7 @@ namespace kuu {
if (UPNPCOMMAND_SUCCESS != r) {
std::ostringstream oss;
oss << "Error in UPNP_AddPortMapping(\"" <<
m_local_data->urls->urls.controlURL << "\", \"" <<
m_local_data->urls->controlURL << "\", \"" <<
m_local_data->data.first.servicetype << "\", " <<
parExtPort << ", " <<
parIntPort << ", \"" <<