From 4f708eb8bbc80e5107fc3679b9f7ccac51883782 Mon Sep 17 00:00:00 2001 From: Raghavendra G Date: Sat, 4 Sep 2010 04:07:58 +0000 Subject: rpc-transport/socket: fix memory leaks. - In the course of reading a single rpc-msg, socket_proto_state_machine may be executed multiple times (since sockets are non-blocking), hence wherever memory is allocated or referenced, checks should be added whether the memory is already allocated or referenced. Signed-off-by: Raghavendra G Signed-off-by: Vijay Bellur BUG: 1438 (memory leaks) URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=1438 --- rpc/rpc-transport/socket/src/socket.c | 46 +++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'rpc/rpc-transport/socket') diff --git a/rpc/rpc-transport/socket/src/socket.c b/rpc/rpc-transport/socket/src/socket.c index f7d4d8e40d0..d3167fe0b72 100644 --- a/rpc/rpc-transport/socket/src/socket.c +++ b/rpc/rpc-transport/socket/src/socket.c @@ -1213,6 +1213,7 @@ __socket_read_reply (rpc_transport_t *this) char *buf = NULL; int32_t ret = -1; rpc_request_info_t *request_info = NULL; + char map_xid = 0; if (!this || !this->private) goto out; @@ -1221,33 +1222,42 @@ __socket_read_reply (rpc_transport_t *this) buf = rpc_xid_addr (iobuf_ptr (priv->incoming.iobuf)); - request_info = GF_CALLOC (1, sizeof (*request_info), gf_common_mt_rpc_trans_reqinfo_t); - if (request_info == NULL) { - gf_log (this->name, GF_LOG_ERROR, "out of memory"); - goto out; + if (priv->incoming.request_info == NULL) { + priv->incoming.request_info = GF_CALLOC (1, + sizeof (*request_info), + gf_common_mt_rpc_trans_reqinfo_t); + if (priv->incoming.request_info == NULL) { + gf_log (this->name, GF_LOG_ERROR, "out of memory"); + goto out; + } + + map_xid = 1; } - priv->incoming.request_info = request_info; + request_info = priv->incoming.request_info; - request_info->xid = ntoh32 (*((uint32_t *) buf)); + if (map_xid) { + request_info->xid = ntoh32 (*((uint32_t *) buf)); - /* release priv->lock, so as to avoid deadlock b/w conn->lock and - * priv->lock, since we are doing an upcall here. - */ - pthread_mutex_unlock (&priv->lock); - { - ret = rpc_transport_notify (this, RPC_TRANSPORT_MAP_XID_REQUEST, - priv->incoming.request_info); - } - pthread_mutex_lock (&priv->lock); + /* release priv->lock, so as to avoid deadlock b/w conn->lock + * and priv->lock, since we are doing an upcall here. + */ + pthread_mutex_unlock (&priv->lock); + { + ret = rpc_transport_notify (this, + RPC_TRANSPORT_MAP_XID_REQUEST, + priv->incoming.request_info); + } + pthread_mutex_lock (&priv->lock); - if (ret == -1) { - goto out; + if (ret == -1) { + goto out; + } } if ((request_info->prognum == GLUSTER3_1_FOP_PROGRAM) && (request_info->procnum == GF_FOP_READ)) { - if (request_info->rsp.rsp_payload_count != 0) { + if (map_xid && request_info->rsp.rsp_payload_count != 0) { priv->incoming.iobref = iobref_ref (request_info->rsp.rsp_iobref); priv->incoming.payload_vector -- cgit