[dpdk-dev,1/2] rte_ethdev: fix crash if malloc fails in rte_eth_dev_callback_register

Message ID 1437090444-24953-2-git-send-email-stephen@networkplumber.org (mailing list archive)
State Accepted, archived
Headers

Commit Message

Stephen Hemminger July 16, 2015, 11:47 p.m. UTC
  Found by coccinelle script.  If rte_zmalloc() failed in rte_eth_dev_callback_register
then NULL pointer would be dereferenced.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/librte_ether/rte_ethdev.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
  

Comments

Bruce Richardson July 17, 2015, 8:16 a.m. UTC | #1
On Thu, Jul 16, 2015 at 04:47:23PM -0700, Stephen Hemminger wrote:
> Found by coccinelle script.  If rte_zmalloc() failed in rte_eth_dev_callback_register
> then NULL pointer would be dereferenced.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  lib/librte_ether/rte_ethdev.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/librte_ether/rte_ethdev.c b/lib/librte_ether/rte_ethdev.c
> index ddf3658..aa363be 100644
> --- a/lib/librte_ether/rte_ethdev.c
> +++ b/lib/librte_ether/rte_ethdev.c
> @@ -2929,9 +2929,10 @@ rte_eth_dev_callback_register(uint8_t port_id,
>  	}
>  
>  	/* create a new callback. */
> -	if (user_cb == NULL &&
> -	    (user_cb = rte_zmalloc("INTR_USER_CALLBACK",
> -				   sizeof(struct rte_eth_dev_callback), 0))) {
> +	if (!user_cb)

Minor style issue. Since user_cb is a pointer, not a boolean, the condition
should use "== NULL" rather than "!".

/Bruce
  
Thomas Monjalon July 22, 2015, 1:54 p.m. UTC | #2
2015-07-17 09:16, Bruce Richardson:
> On Thu, Jul 16, 2015 at 04:47:23PM -0700, Stephen Hemminger wrote:
> > -	if (user_cb == NULL &&
> > -	    (user_cb = rte_zmalloc("INTR_USER_CALLBACK",
> > -				   sizeof(struct rte_eth_dev_callback), 0))) {
> > +	if (!user_cb)
> 
> Minor style issue. Since user_cb is a pointer, not a boolean, the condition
> should use "== NULL" rather than "!".

Fixed before pushing.
  

Patch

diff --git a/lib/librte_ether/rte_ethdev.c b/lib/librte_ether/rte_ethdev.c
index ddf3658..aa363be 100644
--- a/lib/librte_ether/rte_ethdev.c
+++ b/lib/librte_ether/rte_ethdev.c
@@ -2929,9 +2929,10 @@  rte_eth_dev_callback_register(uint8_t port_id,
 	}
 
 	/* create a new callback. */
-	if (user_cb == NULL &&
-	    (user_cb = rte_zmalloc("INTR_USER_CALLBACK",
-				   sizeof(struct rte_eth_dev_callback), 0))) {
+	if (!user_cb)
+	    user_cb = rte_zmalloc("INTR_USER_CALLBACK",
+				  sizeof(struct rte_eth_dev_callback), 0);
+	if (user_cb) {
 		user_cb->cb_fn = cb_fn;
 		user_cb->cb_arg = cb_arg;
 		user_cb->event = event;