Skip to content

Commit

Permalink
fix: memory leak on unix udp multicast
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Feb 15, 2024
1 parent 9bb6f8c commit a40aeb7
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/zenoh-pico/system/link/udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin
int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, uint32_t tout,
const char *iface, const char *join);
void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend,
const _z_sys_net_endpoint_t rep);
const _z_sys_net_endpoint_t rep, const _z_sys_net_endpoint_t lep);
size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len,
const _z_sys_net_endpoint_t lep, _z_bytes_t *ep);
size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep,
Expand Down
3 changes: 2 additions & 1 deletion src/link/multicast/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ int8_t _z_f_link_listen_udp_multicast(_z_link_t *self) {
}

void _z_f_link_close_udp_multicast(_z_link_t *self) {
_z_close_udp_multicast(&self->_socket._udp._sock, &self->_socket._udp._msock, self->_socket._udp._rep);
_z_close_udp_multicast(&self->_socket._udp._sock, &self->_socket._udp._msock, self->_socket._udp._rep,
self->_socket._udp._lep);
}

void _z_f_link_free_udp_multicast(_z_link_t *self) {
Expand Down
3 changes: 2 additions & 1 deletion src/system/arduino/esp32/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo
}

void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend,
const _z_sys_net_endpoint_t rep) {
const _z_sys_net_endpoint_t rep, const _z_sys_net_endpoint_t lep) {
_ZP_UNUSED(lep);
if (rep._iptcp->ai_family == AF_INET) {
struct ip_mreq mreq;
(void)memset(&mreq, 0, sizeof(mreq));
Expand Down
5 changes: 3 additions & 2 deletions src/system/arduino/opencr/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo
}

void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend,
const _z_sys_net_endpoint_t rep) {
(void)(rep);
const _z_sys_net_endpoint_t rep, const _z_sys_net_endpoint_t lep) {
_ZP_UNUSED(rep);
_ZP_UNUSED(lep);

sockrecv->_udp->stop();
delete sockrecv->_udp;
Expand Down
3 changes: 2 additions & 1 deletion src/system/espidf/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo
}

void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend,
const _z_sys_net_endpoint_t rep) {
const _z_sys_net_endpoint_t rep, const _z_sys_net_endpoint_t lep) {
_ZP_UNUSED(lep);
if (rep._iptcp->ai_family == AF_INET) {
struct ip_mreq mreq;
(void)memset(&mreq, 0, sizeof(mreq));
Expand Down
3 changes: 2 additions & 1 deletion src/system/mbed/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo
}

void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend,
const _z_sys_net_endpoint_t rep) {
const _z_sys_net_endpoint_t rep, const _z_sys_net_endpoint_t lep) {
_ZP_UNUSED(lep);
sockrecv->_udp->leave_multicast_group(*rep._iptcp);
sockrecv->_udp->close();
delete sockrecv->_udp;
Expand Down
16 changes: 6 additions & 10 deletions src/system/unix/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,6 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin
laddr->ai_canonname = NULL;
laddr->ai_next = NULL;
lep->_iptcp = laddr;

// This is leaking 16 bytes according to valgrind, but it is a know problem on some libc6
// implementations:
// https://lists.debian.org/debian-glibc/2016/03/msg00241.html
// To avoid a fix to break zenoh-pico, we are let it leak for the moment.
// #if defined(ZENOH_LINUX)
// zp_free(lsockaddr);
// #endif
} else {
ret = _Z_ERR_GENERIC;
}
Expand Down Expand Up @@ -489,7 +481,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo
}

void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend,
const _z_sys_net_endpoint_t rep) {
const _z_sys_net_endpoint_t rep, const _z_sys_net_endpoint_t lep) {
if (rep._iptcp->ai_family == AF_INET) {
struct ip_mreq mreq;
(void)memset(&mreq, 0, sizeof(mreq));
Expand All @@ -507,7 +499,11 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *
// Do nothing. It must never not enter here.
// Required to be compliant with MISRA 15.7 rule
}

#if defined(ZENOH_LINUX)
zp_free(lep._iptcp->ai_addr);
#else
_ZP_UNUSED(lep);
#endif
close(sockrecv->_fd);
close(socksend->_fd);
}
Expand Down
3 changes: 2 additions & 1 deletion src/system/zephyr/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo
}

void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend,
const _z_sys_net_endpoint_t rep) {
const _z_sys_net_endpoint_t rep, const _z_sys_net_endpoint_t lep) {
_ZP_UNUSED(lep);
// FIXME: iface passed into the locator is being ignored
// default if used instead
struct net_if *ifa = NULL;
Expand Down

0 comments on commit a40aeb7

Please sign in to comment.