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

UPNP: Use local variable for UPNPUrls to stop memory leak. #89897

Merged
merged 1 commit into from
Apr 6, 2024
Merged
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
4 changes: 2 additions & 2 deletions modules/upnp/doc_classes/UPNPDevice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<constant name="IGD_STATUS_HTTP_EMPTY" value="2" enum="IGDStatus">
Empty HTTP response.
</constant>
<constant name="IGD_STATUS_NO_URLS" value="3" enum="IGDStatus">
<constant name="IGD_STATUS_NO_URLS" value="3" enum="IGDStatus" deprecated="This value is no longer used.">
Returned response contained no URLs.
</constant>
<constant name="IGD_STATUS_NO_IGD" value="4" enum="IGDStatus">
Expand All @@ -86,7 +86,7 @@
<constant name="IGD_STATUS_INVALID_CONTROL" value="7" enum="IGDStatus">
Invalid control.
</constant>
<constant name="IGD_STATUS_MALLOC_ERROR" value="8" enum="IGDStatus">
<constant name="IGD_STATUS_MALLOC_ERROR" value="8" enum="IGDStatus" deprecated="This value is no longer used.">
Memory allocation error.
</constant>
<constant name="IGD_STATUS_UNKNOWN_ERROR" value="9" enum="IGDStatus">
Expand Down
29 changes: 8 additions & 21 deletions modules/upnp/upnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,33 +121,20 @@ void UPNP::parse_igd(Ref<UPNPDevice> dev, UPNPDev *devlist) {
return;
}

struct UPNPUrls *urls = (UPNPUrls *)malloc(sizeof(struct UPNPUrls));

if (!urls) {
dev->set_igd_status(UPNPDevice::IGD_STATUS_MALLOC_ERROR);
return;
}

struct UPNPUrls urls = {};
struct IGDdatas data;

memset(urls, 0, sizeof(struct UPNPUrls));

parserootdesc(xml, size, &data);
free(xml);
xml = nullptr;

GetUPNPUrls(urls, &data, dev->get_description_url().utf8().get_data(), 0);

if (!urls) {
dev->set_igd_status(UPNPDevice::IGD_STATUS_NO_URLS);
return;
}
Comment on lines -141 to -144
Copy link
Member

Choose a reason for hiding this comment

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

With the removal of this check, and of the previous one, IGD_STATUS_MALLOC_ERROR and IGD_STATUS_NO_URL are unused, and could be unexposed.

Unexposing them would break the API compatibility, but they can at least be flagged as deprecated and their documentation changed to clarify that they're never used.

GetUPNPUrls(&urls, &data, dev->get_description_url().utf8().get_data(), 0);

char addr[16];
int i = UPNP_GetValidIGD(devlist, urls, &data, (char *)&addr, 16);
int i = UPNP_GetValidIGD(devlist, &urls, &data, (char *)&addr, 16);

if (i != 1) {
FreeUPNPUrls(urls);
FreeUPNPUrls(&urls);

switch (i) {
case 0:
Expand All @@ -165,18 +152,18 @@ void UPNP::parse_igd(Ref<UPNPDevice> dev, UPNPDev *devlist) {
}
}

if (urls->controlURL[0] == '\0') {
FreeUPNPUrls(urls);
if (urls.controlURL[0] == '\0') {
FreeUPNPUrls(&urls);
dev->set_igd_status(UPNPDevice::IGD_STATUS_INVALID_CONTROL);
return;
}

dev->set_igd_control_url(urls->controlURL);
dev->set_igd_control_url(urls.controlURL);
dev->set_igd_service_type(data.first.servicetype);
dev->set_igd_our_addr(addr);
dev->set_igd_status(UPNPDevice::IGD_STATUS_OK);

FreeUPNPUrls(urls);
FreeUPNPUrls(&urls);
}

int UPNP::upnp_result(int in) {
Expand Down
Loading