IFCONFIG(8): Fix some resource leaks

This commit is contained in:
Sebastian Huber 2013-10-22 10:03:10 +02:00
parent 60618d5332
commit b833cc4c4a
5 changed files with 54 additions and 4 deletions

View File

@ -70,8 +70,10 @@ list_cloners(void)
ifcr.ifcr_count = ifcr.ifcr_total;
ifcr.ifcr_buffer = buf;
if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0) {
free(buf);
err(1, "SIOCIFGCLONERS for names");
}
/*
* In case some disappeared in the mean time, clamp it down.
@ -104,6 +106,11 @@ clone_setdefcallback(const char *ifprefix, clone_callback_func *p)
struct clone_defcb *dcp;
dcp = malloc(sizeof(*dcp));
#ifndef __rtems__
if (dcp == NULL) {
errx(1, "unable to allocate clone");
}
#endif /* __rtems__ */
strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1);
dcp->clone_cb = p;
SLIST_INSERT_HEAD(&clone_defcbh, dcp, next);
@ -199,3 +206,15 @@ clone_ctor(void)
opt_register(&clone_Copt);
#undef N
}
#ifdef __rtems__
void
clone_dtor(void)
{
struct clone_defcb *dcp;
struct clone_defcb *dcp_tmp;
SLIST_FOREACH_SAFE(dcp, &clone_defcbh, next, dcp_tmp) {
free(dcp);
}
}
#endif /* __rtems__ */

View File

@ -150,6 +150,7 @@ usage(void)
#ifdef __rtems__
static void ifconfig_ctor(void);
static void ifconfig_dtor(void);
static int main(int argc, char *argv[]);
int rtems_bsd_command_ifconfig(int argc, char *argv[])
@ -179,6 +180,9 @@ int rtems_bsd_command_ifconfig(int argc, char *argv[])
exit_code = rtems_bsd_program_call_main("ifconfig", main, argc, argv);
clone_dtor();
ifconfig_dtor();
rtems_bsd_program_unlock();
return exit_code;
@ -1241,3 +1245,19 @@ ifconfig_ctor(void)
cmd_register(&basic_cmds[i]);
#undef N
}
#ifdef __rtems__
static void
ifconfig_dtor(void)
{
struct callback *cb = callbacks;
while (cb != NULL) {
struct callback *to_free = cb;
cb = to_free->cb_next;
free(to_free);
}
free(descr);
}
#endif /* __rtems__ */

View File

@ -169,4 +169,6 @@ void mac_ctor(void);
void nd6_ctor(void);
void pfsync_ctor(void);
void vlan_ctor(void);
void clone_dtor(void);
#endif /* __rtems__ */

View File

@ -137,6 +137,7 @@ printgroup(const char *groupname)
bzero(&ifgr, sizeof(ifgr));
strlcpy(ifgr.ifgr_name, groupname, sizeof(ifgr.ifgr_name));
if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
close(s);
if (errno == EINVAL || errno == ENOTTY ||
errno == ENOENT)
exit(0);
@ -145,10 +146,15 @@ printgroup(const char *groupname)
}
len = ifgr.ifgr_len;
if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
if ((ifgr.ifgr_groups = calloc(1, len)) == NULL) {
close(s);
err(1, "printgroup");
if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
}
if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
free(ifgr.ifgr_groups);
close(s);
err(1, "SIOCGIFGMEMB");
}
for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req);
ifg++) {
@ -157,6 +163,7 @@ printgroup(const char *groupname)
cnt++;
}
free(ifgr.ifgr_groups);
close(s);
exit(0);
}

View File

@ -134,8 +134,10 @@ media_status(int s)
err(1, "malloc");
ifmr.ifm_ulist = media_list;
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
free(media_list);
err(1, "SIOCGIFMEDIA");
}
printf("\tmedia: ");
print_media_word(ifmr.ifm_current, 1);