Skip to content

Commit 2457cf1

Browse files
committed
Fix fd leaks and a possible NULL dereference
Found by coverity
1 parent d1bc2dc commit 2457cf1

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/util.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ get_iface_ip(const char *ifname)
169169
/* Get the IP address */
170170
if (ioctl (sockd, SIOCGIFADDR, &if_data) < 0) {
171171
debug(LOG_ERR, "ioctl(): SIOCGIFADDR %s", strerror(errno));
172+
close(sockd);
172173
return NULL;
173174
}
174175
memcpy ((void *) &ip, (void *) &if_data.ifr_addr.sa_data + 2, 4);
@@ -230,11 +231,16 @@ get_ext_iface(void)
230231
debug(LOG_DEBUG, "get_ext_iface(): Autodectecting the external interface from routing table");
231232
while(keep_detecting) {
232233
input = fopen("/proc/net/route", "r");
234+
if (NULL == input) {
235+
debug(LOG_ERR, "Could not open /proc/net/route (%s).", strerror(errno));
236+
return NULL;
237+
}
233238
while (!feof(input)) {
234239
/* XXX scanf(3) is unsafe, risks overrun */
235240
if ((fscanf(input, "%s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n", device, gw) == 2) && strcmp(gw, "00000000") == 0) {
236241
free(gw);
237242
debug(LOG_INFO, "get_ext_iface(): Detected %s as the default interface after try %d", device, i);
243+
fclose(input);
238244
return device;
239245
}
240246
}

0 commit comments

Comments
 (0)