Hi,
I am experiencing a memory leak and it looks like the cause is in net-snmp
5.4 lib used in my program. I checked net-snmp source code and found that a
buffer was not deleted after use:
File:
net-snmp-5.4\snmplib\snmpUDPDomain.c
Function:
netsnmp_transport *
netsnmp_udp_transport(struct sockaddr_in *addr, int local)
Line:
addr_pair = (netsnmp_udp_addr_pair *)
malloc(sizeof(netsnmp_udp_addr_pair));
The variable addr_pair was not deleted after use. See attached file for the
whole function.
Please advise if this is a bug and need to be fixed.
Thanks,
Luu
netsnmp_transport *
netsnmp_udp_transport(struct sockaddr_in *addr, int local)
{
netsnmp_transport *t = NULL;
int rc = 0;
char *str = NULL;
char *client_socket = NULL;
netsnmp_udp_addr_pair *addr_pair = NULL;
if (addr == NULL || addr->sin_family != AF_INET) {
return NULL;
}
addr_pair = (netsnmp_udp_addr_pair *) malloc(sizeof(netsnmp_udp_addr_pair));
if (addr_pair == NULL) {
return NULL;
}
memset(addr_pair, 0, sizeof(netsnmp_udp_addr_pair));
memcpy(&(addr_pair->remote_addr), addr, sizeof(struct sockaddr_in));
t = (netsnmp_transport *) malloc(sizeof(netsnmp_transport));
if (t == NULL) {
return NULL;
}
str = netsnmp_udp_fmtaddr(NULL, (void *)addr_pair,
sizeof(netsnmp_udp_addr_pair));
DEBUGMSGTL(("netsnmp_udp", "open %s %s:%d\n", local ? "local" : "remote",
str,addr->sin_port));
free(str);
memset(t, 0, sizeof(netsnmp_transport));
t->domain = netsnmpUDPDomain;
t->domain_length = netsnmpUDPDomain_len;
t->sock = socket(PF_INET, SOCK_DGRAM, 0);
if (t->sock < 0) {
netsnmp_transport_free(t);
return NULL;
}
_netsnmp_udp_sockopt_set(t->sock, local);
if (local) {
/*
* This session is inteneded as a server, so we must bind on to the
* given IP address, which may include an interface address, or could
* be INADDR_ANY, but certainly includes a port number.
*/
t->local = (u_char *) malloc(6);
if (t->local == NULL) {
netsnmp_transport_free(t);
return NULL;
}
memcpy(t->local, (u_char *) & (addr->sin_addr.s_addr), 4);
t->local[4] = (htons(addr->sin_port) & 0xff00) >> 8;
t->local[5] = (htons(addr->sin_port) & 0x00ff) >> 0;
t->local_length = 6;
#if defined(linux) && defined(IP_PKTINFO)
{
int sockopt = 1;
if (setsockopt(t->sock, SOL_IP, IP_PKTINFO, &sockopt, sizeof
sockopt) == -1) {
DEBUGMSGTL(("netsnmp_udp", "couldn't set IP_PKTINFO: %s\n",
strerror(errno)));
return NULL;
}
DEBUGMSGTL(("netsnmp_udp", "set IP_PKTINFO\n"));
}
#endif
rc = bind(t->sock, (struct sockaddr *) addr,
sizeof(struct sockaddr));
if (rc != 0) {
netsnmp_udp_close(t);
netsnmp_transport_free(t);
return NULL;
}
t->data = NULL;
t->data_length = 0;
} else {
/*
* This is a client session. If we've been given a
* client address to send from, then bind to that.
* Otherwise the send will use "something sensible".
*/
client_socket = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
NETSNMP_DS_LIB_CLIENT_ADDR);
if (client_socket) {
struct sockaddr_in client_addr;
netsnmp_sockaddr_in2(&client_addr, client_socket, NULL);
client_addr.sin_port = 0;
bind(t->sock, (struct sockaddr *)&client_addr,
sizeof(struct sockaddr));
}
/*
* Save the (remote) address in the
* transport-specific data pointer for later use by netsnmp_udp_send.
*/
t->data = malloc(sizeof(netsnmp_udp_addr_pair));
t->remote = (u_char *)malloc(6);
if (t->data == NULL || t->remote == NULL) {
netsnmp_transport_free(t);
return NULL;
}
memcpy(t->remote, (u_char *) & (addr->sin_addr.s_addr), 4);
t->remote[4] = (htons(addr->sin_port) & 0xff00) >> 8;
t->remote[5] = (htons(addr->sin_port) & 0x00ff) >> 0;
t->remote_length = 6;
memcpy(t->data, addr_pair, sizeof(netsnmp_udp_addr_pair));
t->data_length = sizeof(netsnmp_udp_addr_pair);
}
/*
* 16-bit length field, 8 byte UDP header, 20 byte IPv4 header
*/
t->msgMaxSize = 0xffff - 8 - 20;
t->f_recv = netsnmp_udp_recv;
t->f_send = netsnmp_udp_send;
t->f_close = netsnmp_udp_close;
t->f_accept = NULL;
t->f_fmtaddr = netsnmp_udp_fmtaddr;
return t;
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders