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