[dpdk-dev,v7] af_packet: Fix some klocwork errors

Message ID 1426037641-4515-1-git-send-email-changchun.ouyang@intel.com (mailing list archive)
State Accepted, archived
Headers

Commit Message

Ouyang Changchun March 11, 2015, 1:34 a.m. UTC
  Fix possible memory leak issue: free kvlist before return;
Fix possible resource lost issue: close qssockfd before return;

Signed-off-by: Changchun Ouyang <changchun.ouyang@intel.com>
---
Change in v7:
  - Remove unnecessary '!= NULL' check before freeing it; 

Change in v6:
  - Refine exit point;
 
Change in v5:
  - Initialize qsockfd with -1;
 
Change in v4:
  - Check sockfd in internals->rx_queue against 0;
 
Change in v3:
  - Also close sockets for all queues;
 
Change in v2:
  - Make the error exit point a common path.

 lib/librte_pmd_af_packet/rte_eth_af_packet.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
  

Comments

Michael Qiu March 12, 2015, 10:43 a.m. UTC | #1
On 3/11/2015 9:34 AM, Ouyang Changchun wrote:
> Fix possible memory leak issue: free kvlist before return;
> Fix possible resource lost issue: close qssockfd before return;
>
> Signed-off-by: Changchun Ouyang <changchun.ouyang@intel.com>
> ---
> Change in v7:
>   - Remove unnecessary '!= NULL' check before freeing it; 
>
> Change in v6:
>   - Refine exit point;
>  
> Change in v5:
>   - Initialize qsockfd with -1;
>  
> Change in v4:
>   - Check sockfd in internals->rx_queue against 0;
>  
> Change in v3:
>   - Also close sockets for all queues;
>  
> Change in v2:
>   - Make the error exit point a common path.

Acked-by: Michael Qiu <michael.qiu@intel.com>
>  lib/librte_pmd_af_packet/rte_eth_af_packet.c | 25 ++++++++++++++++---------
>  1 file changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/lib/librte_pmd_af_packet/rte_eth_af_packet.c b/lib/librte_pmd_af_packet/rte_eth_af_packet.c
> index 80e9bdf..2ac50ba 100644
> --- a/lib/librte_pmd_af_packet/rte_eth_af_packet.c
> +++ b/lib/librte_pmd_af_packet/rte_eth_af_packet.c
> @@ -442,7 +442,8 @@ rte_pmd_init_internals(const char *name,
>  	struct tpacket_req *req;
>  	struct pkt_rx_queue *rx_queue;
>  	struct pkt_tx_queue *tx_queue;
> -	int rc, qsockfd, tpver, discard;
> +	int rc, tpver, discard;
> +	int qsockfd = -1;
>  	unsigned int i, q, rdsize;
>  	int fanout_arg __rte_unused, bypass __rte_unused;
>  
> @@ -691,9 +692,14 @@ error:
>  				rte_free((*internals)->rx_queue[q].rd);
>  			if ((*internals)->tx_queue[q].rd)
>  				rte_free((*internals)->tx_queue[q].rd);
> +			if (((*internals)->rx_queue[q].sockfd != 0) &&
> +				((*internals)->rx_queue[q].sockfd != qsockfd))
> +				close((*internals)->rx_queue[q].sockfd);
>  		}
>  		rte_free(*internals);
>  	}
> +	if (qsockfd != -1)
> +		close(qsockfd);
>  	return -1;
>  }
>  
> @@ -802,7 +808,7 @@ int
>  rte_pmd_af_packet_devinit(const char *name, const char *params)
>  {
>  	unsigned numa_node;
> -	int ret;
> +	int ret = 0;
>  	struct rte_kvargs *kvlist;
>  	int sockfd = -1;
>  
> @@ -811,8 +817,10 @@ rte_pmd_af_packet_devinit(const char *name, const char *params)
>  	numa_node = rte_socket_id();
>  
>  	kvlist = rte_kvargs_parse(params, valid_arguments);
> -	if (kvlist == NULL)
> -		return -1;
> +	if (kvlist == NULL) {
> +		ret = -1;
> +		goto exit;
> +	}
>  
>  	/*
>  	 * If iface argument is passed we open the NICs and use them for
> @@ -823,16 +831,15 @@ rte_pmd_af_packet_devinit(const char *name, const char *params)
>  		ret = rte_kvargs_process(kvlist, ETH_AF_PACKET_IFACE_ARG,
>  		                         &open_packet_iface, &sockfd);
>  		if (ret < 0)
> -			return -1;
> +			goto exit;
>  	}
>  
>  	ret = rte_eth_from_packet(name, &sockfd, numa_node, kvlist);
>  	close(sockfd); /* no longer needed */
>  
> -	if (ret < 0)
> -		return -1;
> -
> -	return 0;
> +exit:
> +	rte_kvargs_free(kvlist);
> +	return ret;
>  }
>  
>  static struct rte_driver pmd_af_packet_drv = {
  
Neil Horman March 12, 2015, 1:38 p.m. UTC | #2
On Thu, Mar 12, 2015 at 10:43:24AM +0000, Qiu, Michael wrote:
> On 3/11/2015 9:34 AM, Ouyang Changchun wrote:
> > Fix possible memory leak issue: free kvlist before return;
> > Fix possible resource lost issue: close qssockfd before return;
> >
> > Signed-off-by: Changchun Ouyang <changchun.ouyang@intel.com>
> > ---
> > Change in v7:
> >   - Remove unnecessary '!= NULL' check before freeing it; 
> >
> > Change in v6:
> >   - Refine exit point;
> >  
> > Change in v5:
> >   - Initialize qsockfd with -1;
> >  
> > Change in v4:
> >   - Check sockfd in internals->rx_queue against 0;
> >  
> > Change in v3:
> >   - Also close sockets for all queues;
> >  
> > Change in v2:
> >   - Make the error exit point a common path.
> 
> Acked-by: Michael Qiu <michael.qiu@intel.com>
Acked-by: Neil Horman <nhorman@tuxdriver.com>
  
John W. Linville March 12, 2015, 5:09 p.m. UTC | #3
On Thu, Mar 12, 2015 at 09:38:46AM -0400, Neil Horman wrote:
> On Thu, Mar 12, 2015 at 10:43:24AM +0000, Qiu, Michael wrote:
> > On 3/11/2015 9:34 AM, Ouyang Changchun wrote:
> > > Fix possible memory leak issue: free kvlist before return;
> > > Fix possible resource lost issue: close qssockfd before return;
> > >
> > > Signed-off-by: Changchun Ouyang <changchun.ouyang@intel.com>
> > > ---
> > > Change in v7:
> > >   - Remove unnecessary '!= NULL' check before freeing it; 
> > >
> > > Change in v6:
> > >   - Refine exit point;
> > >  
> > > Change in v5:
> > >   - Initialize qsockfd with -1;
> > >  
> > > Change in v4:
> > >   - Check sockfd in internals->rx_queue against 0;
> > >  
> > > Change in v3:
> > >   - Also close sockets for all queues;
> > >  
> > > Change in v2:
> > >   - Make the error exit point a common path.
> > 
> > Acked-by: Michael Qiu <michael.qiu@intel.com>
> Acked-by: Neil Horman <nhorman@tuxdriver.com>

Acked-by: John W. Linville <linville@tuxdriver.com>
  
Thomas Monjalon March 17, 2015, 9:34 p.m. UTC | #4
> > > > Fix possible memory leak issue: free kvlist before return;
> > > > Fix possible resource lost issue: close qssockfd before return;
> > > >
> > > > Signed-off-by: Changchun Ouyang <changchun.ouyang@intel.com>
> > > > ---
> > > > Change in v7:
> > > >   - Remove unnecessary '!= NULL' check before freeing it; 
> > > >
> > > > Change in v6:
> > > >   - Refine exit point;
> > > >  
> > > > Change in v5:
> > > >   - Initialize qsockfd with -1;
> > > >  
> > > > Change in v4:
> > > >   - Check sockfd in internals->rx_queue against 0;
> > > >  
> > > > Change in v3:
> > > >   - Also close sockets for all queues;
> > > >  
> > > > Change in v2:
> > > >   - Make the error exit point a common path.
> > > 
> > > Acked-by: Michael Qiu <michael.qiu@intel.com>
> > Acked-by: Neil Horman <nhorman@tuxdriver.com>
> 
> Acked-by: John W. Linville <linville@tuxdriver.com>

Applied, thanks
  

Patch

diff --git a/lib/librte_pmd_af_packet/rte_eth_af_packet.c b/lib/librte_pmd_af_packet/rte_eth_af_packet.c
index 80e9bdf..2ac50ba 100644
--- a/lib/librte_pmd_af_packet/rte_eth_af_packet.c
+++ b/lib/librte_pmd_af_packet/rte_eth_af_packet.c
@@ -442,7 +442,8 @@  rte_pmd_init_internals(const char *name,
 	struct tpacket_req *req;
 	struct pkt_rx_queue *rx_queue;
 	struct pkt_tx_queue *tx_queue;
-	int rc, qsockfd, tpver, discard;
+	int rc, tpver, discard;
+	int qsockfd = -1;
 	unsigned int i, q, rdsize;
 	int fanout_arg __rte_unused, bypass __rte_unused;
 
@@ -691,9 +692,14 @@  error:
 				rte_free((*internals)->rx_queue[q].rd);
 			if ((*internals)->tx_queue[q].rd)
 				rte_free((*internals)->tx_queue[q].rd);
+			if (((*internals)->rx_queue[q].sockfd != 0) &&
+				((*internals)->rx_queue[q].sockfd != qsockfd))
+				close((*internals)->rx_queue[q].sockfd);
 		}
 		rte_free(*internals);
 	}
+	if (qsockfd != -1)
+		close(qsockfd);
 	return -1;
 }
 
@@ -802,7 +808,7 @@  int
 rte_pmd_af_packet_devinit(const char *name, const char *params)
 {
 	unsigned numa_node;
-	int ret;
+	int ret = 0;
 	struct rte_kvargs *kvlist;
 	int sockfd = -1;
 
@@ -811,8 +817,10 @@  rte_pmd_af_packet_devinit(const char *name, const char *params)
 	numa_node = rte_socket_id();
 
 	kvlist = rte_kvargs_parse(params, valid_arguments);
-	if (kvlist == NULL)
-		return -1;
+	if (kvlist == NULL) {
+		ret = -1;
+		goto exit;
+	}
 
 	/*
 	 * If iface argument is passed we open the NICs and use them for
@@ -823,16 +831,15 @@  rte_pmd_af_packet_devinit(const char *name, const char *params)
 		ret = rte_kvargs_process(kvlist, ETH_AF_PACKET_IFACE_ARG,
 		                         &open_packet_iface, &sockfd);
 		if (ret < 0)
-			return -1;
+			goto exit;
 	}
 
 	ret = rte_eth_from_packet(name, &sockfd, numa_node, kvlist);
 	close(sockfd); /* no longer needed */
 
-	if (ret < 0)
-		return -1;
-
-	return 0;
+exit:
+	rte_kvargs_free(kvlist);
+	return ret;
 }
 
 static struct rte_driver pmd_af_packet_drv = {