Subject: | Memory leak in zmq_recv |
Date: | Thu, 9 May 2013 10:12:14 +0200 |
To: | bug-ZMQ-LibZMQ3 [...] rt.cpan.org |
From: | Ton Nijkes <ton.nijkes [...] gmail.com> |
Hi,
I believe there is a memory leak in the P5ZMQ3_zmq_recv function.
A temporary buffer is allocated, but only freed when something goes wrong.
From perl_libzmq3.xs:
int
P5ZMQ3_zmq_recv(socket, buf_sv, len, flags = 0)
P5ZMQ3_Socket *socket;
SV *buf_sv;
size_t len;
int flags;
PREINIT:
char *buf;
CODE:
P5ZMQ3_TRACE( "START zmq_recv" );
Newxz( buf, len, char );
RETVAL = zmq_recv( socket->socket, buf, len, flags );
P5ZMQ3_TRACE(" + zmq_recv returned with rv '%d'", RETVAL);
if ( RETVAL == -1 ) {
SET_BANG;
Safefree(buf);
} else {
sv_setpvn( buf_sv, buf, len );
}
P5ZMQ3_TRACE( "END zmq_recv" );
OUTPUT:
RETVAL
To fix, I suggest placing the Safefree(buf) outside the if-statement:
int
P5ZMQ3_zmq_recv(socket, buf_sv, len, flags = 0)
P5ZMQ3_Socket *socket;
SV *buf_sv;
size_t len;
int flags;
PREINIT:
char *buf;
CODE:
P5ZMQ3_TRACE( "START zmq_recv" );
Newxz( buf, len, char );
RETVAL = zmq_recv( socket->socket, buf, len, flags );
P5ZMQ3_TRACE(" + zmq_recv returned with rv '%d'", RETVAL);
if ( RETVAL == -1 ) {
SET_BANG;
} else {
sv_setpvn( buf_sv, buf, len );
}
Safefree(buf);
P5ZMQ3_TRACE( "END zmq_recv" );
OUTPUT:
RETVAL
Best regards,
Ton Nijkes