Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try resolve #1175 #1223

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/libtorrent/upnp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ struct TORRENT_EXTRA_EXPORT upnp final
// the url to the WANIP or WANPPP interface
std::string control_url;
// either the WANIP namespace or the WANPPP namespace
char const* service_namespace = nullptr;
std::string service_namespace;

std::vector<mapping_t> mapping;

Expand Down
27 changes: 16 additions & 11 deletions src/upnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ int upnp::add_mapping(portmap_protocol const p, int const external_port
m.external_port = external_port;
m.local_port = local_port;

if (d.service_namespace) update_map(d, mapping_index);
if (!d.service_namespace.empty()) update_map(d, mapping_index);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not

if (d.service_namespace && !d.service_namespace.empty()) update_map(d, mapping_index);

and avoid a possible NPE?

The same comment applies everywhere else below, and since it happens so much perhaps I'd add a private utility function to not repeat yourself all over

bool valid_service_namespace(rootdevice &d) {
    return d.service_namespace && !d.service_namespace.empty();
}

and then have

if (valid_service_namespace(d)) update_map(d, mapping_index);

...

TORRENT_ASSERT(valid_service_namespace(d));

...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service_namespace -> std::string
if(string()) - impossible

int main()
{
std::string name;
if(name) {
}
}

In function 'int main()': 8:10: error: could not convert 'name' from 'std::string {aka std::basic_string}' to 'bool'

}

return mapping_index;
Expand Down Expand Up @@ -255,7 +255,7 @@ void upnp::delete_mapping(int mapping)
TORRENT_ASSERT(mapping < int(d.mapping.size()));
d.mapping[mapping].act = mapping_t::action::del;

if (d.service_namespace) update_map(d, mapping);
if (!d.service_namespace.empty()) update_map(d, mapping);
}
}

Expand Down Expand Up @@ -699,7 +699,7 @@ void upnp::post(upnp::rootdevice const& d, char const* soap
"Soapaction: \"%s#%s\"\r\n\r\n"
"%s"
, d.path.c_str(), d.hostname.c_str(), d.port
, int(strlen(soap)), d.service_namespace, soap_action
, int(strlen(soap)), d.service_namespace.c_str(), soap_action
, soap);

d.upnp_connection->m_sendbuffer = header;
Expand Down Expand Up @@ -743,7 +743,7 @@ void upnp::create_port_mapping(http_connection& c, rootdevice& d, int i)
"<NewPortMappingDescription>%s at %s:%d</NewPortMappingDescription>"
"<NewLeaseDuration>%u</NewLeaseDuration>"
"</u:%s></s:Body></s:Envelope>"
, soap_action, d.service_namespace, d.mapping[i].external_port
, soap_action, d.service_namespace.c_str(), d.mapping[i].external_port
, (d.mapping[i].protocol == portmap_protocol::udp ? "UDP" : "TCP")
, d.mapping[i].local_port
, local_endpoint.c_str()
Expand Down Expand Up @@ -795,7 +795,7 @@ void upnp::update_map(rootdevice& d, int i)
}

TORRENT_ASSERT(!d.upnp_connection);
TORRENT_ASSERT(d.service_namespace);
TORRENT_ASSERT(!d.service_namespace.empty());

#ifndef TORRENT_DISABLE_LOGGING
log("connecting to %s", d.hostname.c_str());
Expand Down Expand Up @@ -852,6 +852,8 @@ void upnp::delete_port_mapping(rootdevice& d, int i)

char const* soap_action = "DeletePortMapping";

TORRENT_ASSERT(!d.service_namespace.empty());

char soap[2048];
error_code ec;
std::snprintf(soap, sizeof(soap), "<?xml version=\"1.0\"?>\n"
Expand All @@ -862,7 +864,7 @@ void upnp::delete_port_mapping(rootdevice& d, int i)
"<NewExternalPort>%u</NewExternalPort>"
"<NewProtocol>%s</NewProtocol>"
"</u:%s></s:Body></s:Envelope>"
, soap_action, d.service_namespace
, soap_action, d.service_namespace.c_str()
, d.mapping[i].external_port
, (d.mapping[i].protocol == portmap_protocol::udp ? "UDP" : "TCP")
, soap_action);
Expand Down Expand Up @@ -996,9 +998,10 @@ void upnp::on_upnp_xml(error_code const& e
d.disabled = true;
return;
}
static std::string service_type;
service_type.swap(s.service_type);
d.service_namespace = service_type.c_str();
d.service_namespace = s.service_type;

TORRENT_ASSERT(!d.service_namespace.empty());

if (!s.model.empty()) m_model = s.model;

if (!s.url_base.empty() && s.control_url.substr(0, 7) != "http://")
Expand Down Expand Up @@ -1029,7 +1032,7 @@ void upnp::on_upnp_xml(error_code const& e
{
log("found control URL: %s namespace %s "
"urlbase: %s in response from %s"
, d.control_url.c_str(), d.service_namespace
, d.control_url.c_str(), d.service_namespace.c_str()
, s.url_base.c_str(), d.url.c_str());
}
#endif
Expand Down Expand Up @@ -1077,14 +1080,16 @@ void upnp::get_ip_address(rootdevice& d)

char const* soap_action = "GetExternalIPAddress";

TORRENT_ASSERT(!d.service_namespace.empty());

char soap[2048];
error_code ec;
std::snprintf(soap, sizeof(soap), "<?xml version=\"1.0\"?>\n"
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
"<s:Body><u:%s xmlns:u=\"%s\">"
"</u:%s></s:Body></s:Envelope>"
, soap_action, d.service_namespace
, soap_action, d.service_namespace.c_str()
, soap_action);

post(d, soap, soap_action);
Expand Down