diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2013-10-22 10:03:10 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2013-10-31 13:18:49 +0100 |
commit | b833cc4c4a6e703b851200da046c057bb5cd4a1d (patch) | |
tree | e64e41e77ac454a6f3ad26b8e88895964cb1eddb /freebsd/sbin/ifconfig | |
parent | IFCONFIG(8): Initialize global variables in ctors (diff) | |
download | rtems-libbsd-b833cc4c4a6e703b851200da046c057bb5cd4a1d.tar.bz2 |
IFCONFIG(8): Fix some resource leaks
Diffstat (limited to 'freebsd/sbin/ifconfig')
-rw-r--r-- | freebsd/sbin/ifconfig/ifclone.c | 21 | ||||
-rw-r--r-- | freebsd/sbin/ifconfig/ifconfig.c | 20 | ||||
-rw-r--r-- | freebsd/sbin/ifconfig/ifconfig.h | 2 | ||||
-rw-r--r-- | freebsd/sbin/ifconfig/ifgroup.c | 11 | ||||
-rw-r--r-- | freebsd/sbin/ifconfig/ifmedia.c | 4 |
5 files changed, 54 insertions, 4 deletions
diff --git a/freebsd/sbin/ifconfig/ifclone.c b/freebsd/sbin/ifconfig/ifclone.c index 84ec61bd..2de84c34 100644 --- a/freebsd/sbin/ifconfig/ifclone.c +++ b/freebsd/sbin/ifconfig/ifclone.c @@ -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__ */ diff --git a/freebsd/sbin/ifconfig/ifconfig.c b/freebsd/sbin/ifconfig/ifconfig.c index 3bf8df77..b80d880f 100644 --- a/freebsd/sbin/ifconfig/ifconfig.c +++ b/freebsd/sbin/ifconfig/ifconfig.c @@ -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__ */ diff --git a/freebsd/sbin/ifconfig/ifconfig.h b/freebsd/sbin/ifconfig/ifconfig.h index 291aed63..46d8382e 100644 --- a/freebsd/sbin/ifconfig/ifconfig.h +++ b/freebsd/sbin/ifconfig/ifconfig.h @@ -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__ */ diff --git a/freebsd/sbin/ifconfig/ifgroup.c b/freebsd/sbin/ifconfig/ifgroup.c index f5202fdd..d83eed5a 100644 --- a/freebsd/sbin/ifconfig/ifgroup.c +++ b/freebsd/sbin/ifconfig/ifgroup.c @@ -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); } diff --git a/freebsd/sbin/ifconfig/ifmedia.c b/freebsd/sbin/ifconfig/ifmedia.c index 9a78ea7b..aa262568 100644 --- a/freebsd/sbin/ifconfig/ifmedia.c +++ b/freebsd/sbin/ifconfig/ifmedia.c @@ -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); |