Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bugfix: Fix memory leak when destroying iflist
    In positionptr() when dev_get_ifname() fails memory is leaked.
    Fixed by placing the free() in a wrapper function and calling
    it as needed. Also, added the wrapper functions in all expected
    places.

    Signed-off-by: Phil Cameron <pcameron@redhat.com>
    Acked-by: Vitezslav Samel <vitezslav@samel.cz>
  • Loading branch information
pecameron committed Apr 15, 2016
1 parent d786695 commit 8b9c12a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/ifstats.c
Expand Up @@ -134,6 +134,15 @@ static struct iflist *alloc_iflist_entry(void)
return tmp;
}

static void free_iflist_entry(struct iflist *ptr)
{
if (!ptr)
return;

rate_destroy(&ptr->rate);
free(ptr);
}

/*
* Initialize the list of interfaces. This linked list is used in the
* selection boxes as well as in the general interface statistics screen.
Expand Down Expand Up @@ -224,6 +233,7 @@ static struct iflist *positionptr(struct iflist *iflist, const int ifindex)
int r = dev_get_ifname(ifindex, itmp->ifname);
if (r != 0) {
write_error("Error getting interface name");
free_iflist_entry(itmp);
return(NULL);
}

Expand All @@ -243,8 +253,7 @@ static void destroyiflist(struct iflist *list)
while (ptmp != NULL) {
struct iflist *ctmp = ptmp->next_entry;

rate_destroy(&ptmp->rate);
free(ptmp);
free_iflist_entry(ptmp);
ptmp = ctmp;
}
}
Expand Down Expand Up @@ -663,10 +672,9 @@ void selectiface(char *ifname, int withall, int *aborted)
}

if ((withall) && (list != NULL)) {
ptmp = xmalloc(sizeof(struct iflist));
ptmp = alloc_iflist_entry();
strncpy(ptmp->ifname, "All interfaces", sizeof(ptmp->ifname));
ptmp->ifindex = 0;
rate_alloc(&ptmp->rate, 5); /* FIXME: need iflist_entry_init() */

ptmp->prev_entry = NULL;
list->prev_entry = ptmp;
Expand Down

0 comments on commit 8b9c12a

Please sign in to comment.