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

Message ID 1424998153-22718-1-git-send-email-changchun.ouyang@intel.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

Ouyang Changchun Feb. 27, 2015, 12:49 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 v2:
  - Make the error exit point a common path.

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

Comments

Neil Horman Feb. 27, 2015, 2:04 p.m. UTC | #1
On Fri, Feb 27, 2015 at 08:49:13AM +0800, 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 v2:
>   - Make the error exit point a common path.
> 
>  lib/librte_pmd_af_packet/rte_eth_af_packet.c | 11 +++++++++--
>  1 file changed, 9 insertions(+), 2 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..c675724 100644
> --- a/lib/librte_pmd_af_packet/rte_eth_af_packet.c
> +++ b/lib/librte_pmd_af_packet/rte_eth_af_packet.c
> @@ -694,6 +694,8 @@ error:
>  		}
>  		rte_free(*internals);
>  	}
> +	if (qsockfd != -1)
> +		close(qsockfd);
This is insufficient.  qsockfd is a loop index that is reassigned for each tx
queue we have.  In the error path you need to do this, and loop through the tx
queues, closing each tx_queue->sockfd.

>  	return -1;
>  }
>  
> @@ -823,16 +825,21 @@ 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 error;
>  	}
>  
>  	ret = rte_eth_from_packet(name, &sockfd, numa_node, kvlist);
>  	close(sockfd); /* no longer needed */
>  
>  	if (ret < 0)
> -		return -1;
> +		goto error;
>  
> +	rte_kvargs_free(kvlist);
>  	return 0;
> +
> +error:
> +	rte_kvargs_free(kvlist);
> +	return -1;
>  }
>  
>  static struct rte_driver pmd_af_packet_drv = {
> -- 
> 1.8.4.2
> 
>
  
Ouyang Changchun Feb. 28, 2015, 12:51 a.m. UTC | #2
> -----Original Message-----
> From: Neil Horman [mailto:nhorman@tuxdriver.com]
> Sent: Friday, February 27, 2015 10:05 PM
> To: Ouyang, Changchun
> Cc: dev@dpdk.org
> Subject: Re: [dpdk-dev] [PATCH v2] af_packet: Fix some klocwork errors
> 
> On Fri, Feb 27, 2015 at 08:49:13AM +0800, 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 v2:
> >   - Make the error exit point a common path.
> >
> >  lib/librte_pmd_af_packet/rte_eth_af_packet.c | 11 +++++++++--
> >  1 file changed, 9 insertions(+), 2 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..c675724 100644
> > --- a/lib/librte_pmd_af_packet/rte_eth_af_packet.c
> > +++ b/lib/librte_pmd_af_packet/rte_eth_af_packet.c
> > @@ -694,6 +694,8 @@ error:
> >  		}
> >  		rte_free(*internals);
> >  	}
> > +	if (qsockfd != -1)
> > +		close(qsockfd);
> This is insufficient.  qsockfd is a loop index that is reassigned for each tx
> queue we have.  In the error path you need to do this, and loop through the
> tx queues, closing each tx_queue->sockfd.
> 

Thanks, will have a v3 to resolve it.
Changchun
  

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..c675724 100644
--- a/lib/librte_pmd_af_packet/rte_eth_af_packet.c
+++ b/lib/librte_pmd_af_packet/rte_eth_af_packet.c
@@ -694,6 +694,8 @@  error:
 		}
 		rte_free(*internals);
 	}
+	if (qsockfd != -1)
+		close(qsockfd);
 	return -1;
 }
 
@@ -823,16 +825,21 @@  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 error;
 	}
 
 	ret = rte_eth_from_packet(name, &sockfd, numa_node, kvlist);
 	close(sockfd); /* no longer needed */
 
 	if (ret < 0)
-		return -1;
+		goto error;
 
+	rte_kvargs_free(kvlist);
 	return 0;
+
+error:
+	rte_kvargs_free(kvlist);
+	return -1;
 }
 
 static struct rte_driver pmd_af_packet_drv = {