Skip to content

Commit b391071

Browse files
committed
dns-sd: Fix memleak in dnssd_find_hosts()
Previously, the "buffer" memory was not freed, and WSACleanup() was not called when exiting early. Signed-off-by: Paul Cercueil <[email protected]>
1 parent 1638344 commit b391071

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

dns_sd_windows.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,30 +193,30 @@ int dnssd_find_hosts(struct dns_sd_discovery_data **ddata)
193193
void *buffer;
194194
int sockets[32];
195195
int transaction_id[32];
196-
int ret;
196+
int ret = -ENOMEM;
197197

198198
if (WSAStartup(versionWanted, &wsaData)) {
199199
printf("Failed to initialize WinSock\n");
200-
return -1;
200+
return -WSAGetLastError();
201201
}
202202

203203
IIO_DEBUG("DNS SD: Start service discovery.\n");
204204

205205
*ddata = zalloc(sizeof(**ddata));
206206
if (!*ddata)
207-
return -ENOMEM;
207+
goto out_wsa_cleanup;
208208

209209

210210
buffer = malloc(capacity);
211211
if (!buffer)
212-
return -ENOMEM;
212+
goto out_wsa_cleanup;
213213

214214
IIO_DEBUG("Sending DNS-SD discovery\n");
215215

216216
ret = open_client_sockets(sockets, ARRAY_SIZE(sockets));
217217
if (ret <= 0) {
218218
IIO_ERROR("Failed to open any client sockets\n");
219-
return ret;
219+
goto out_free_buffer;
220220
}
221221

222222
num_sockets = (unsigned int)ret;
@@ -266,10 +266,12 @@ int dnssd_find_hosts(struct dns_sd_discovery_data **ddata)
266266

267267
IIO_DEBUG("Closed socket%s\n", (num_sockets > 1) ? "s" : "");
268268

269+
ret = 0;
270+
out_free_buffer:
269271
free(buffer);
272+
out_wsa_cleanup:
270273
WSACleanup();
271-
272-
return 0;
274+
return ret;
273275
}
274276

275277
int dnssd_resolve_host(const char *hostname, char *ip_addr, const int addr_len)

0 commit comments

Comments
 (0)