tasklwIP - A Lightweight TCP/IP stack - Tasks: task #15205, memory leak in autoip.c

 
 

You are not allowed to post comments on this tracker with your current authentication level.

task #15205: memory leak in autoip.c

Submitter:  zhangyanjiao <anne1993>
Submitted:  Wed 13 Mar 2019 06:21:07 AM UTC
   
 
Category:  IPv4 Should Start On:  Wed 13 Mar 2019 12:00:00 AM UTC
Should be Finished on:  Wed 13 Mar 2019 12:00:00 AM UTC Priority:  5 - Normal
Status:  Cancelled Privacy:  Public
Assigned to:  None Percent Complete:  0%
Open/Closed:  Closed Planned Release:  None
Effort:  0.00

Wed 13 Mar 2019 06:52:31 AM UTC, comment #1: 

This should be reported as a 'bug', not a 'task'. Thanks.

And strictly speaking, this is not a memory leak as the memory will not be allocated again when starting autoip again. The memory just stays allocated. This was good enough for embedded targets. It might not be good enough for lwIP used as a library though.

However, your fix doesn't really work as is, as there's the possibility to allocate the struct autoip from static memory (in situations where lwIP has no heap at all). When this is done, you can't just call mem_free on this pointer.

Simon Goldschmidt <goldsimon>
Group administrator
Wed 13 Mar 2019 06:21:07 AM UTC, original submission:  

When I test the autoip, In find there is memory leak in autoip.c:

In autopip_start() function, mem_malloc is called to alloc memory:
autoip_start(struct netif *netif)
{
  struct autoip* autoip = netif_autoip_data(netif);
  if (autoip == NULL) {
    /* no AutoIP client attached yet? */
    autoip = (struct autoip *)mem_malloc(sizeof(struct autoip));
   ……
}

But in autoip_stop() function, there is no function to free this memory:
err_t
autoip_stop(struct netif *netif)
{
  struct autoip* autoip = netif_autoip_data(netif);

  if (autoip != NULL) {
    autoip->state = AUTOIP_STATE_OFF;
    if (ip4_addr_islinklocal(netif_ip4_addr(netif))) {
      netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
    }
  }
  return ERR_OK;
}

The correct code will be like the following:
err_t
autoip_stop(struct netif *netif)
{
  struct autoip* autoip = netif_autoip_data(netif);

  if (autoip != NULL) {
    autoip->state = AUTOIP_STATE_OFF;
    if (ip4_addr_islinklocal(netif_ip4_addr(netif))) {
      netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
    }
    mem_free(autoip);
    netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, NULL);
  }
  return ERR_OK;
}

zhangyanjiao <anne1993>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by goldsimon (Posted a comment)
  • -email is unavailable- added by anne1993 (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-03-13 goldsimon StatusNone Cancelled
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code