Skip to contentSkip to conversation

drivers/scsi/fcoe/fcoe.c: add missing test

PATCH Applied Julia Lawall 3 participants 7 messages Aug 22, 2011 – Aug 23, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 2/4 drivers/scsi/fcoe/fcoe.c: add missing test

From: Julia Lawall <[email protected]>

The initializations of both fcoe_nport_scsi_transport and fcoe_vport_scsi_transport can fail, so test both of them.

The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@r@
identifier x,y,f!={PTR_ERR,ERR_PTR,ERR_CAST};
statement S;
@@

x = f(...);
(
if (\(x = NULL\|IS_ERR(x)\)) S
|
*if (\(y = NULL\|IS_ERR(y)\))
{ ... when != x
return ...; }
)
// </smpl>

---
 drivers/scsi/fcoe/fcoe.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/fcoe/fcoe.c +1 -1
index ba710e3..921b636 100644
@@ -1096,7 +1096,7 @@ static int __init fcoe_if_init(void)
 	fcoe_vport_scsi_transport  		fc_attach_transport(&fcoe_vport_fc_functions);
 
-	if (!fcoe_nport_scsi_transport) {
+	if (!fcoe_nport_scsi_transport || !fcoe_vport_scsi_transport) {
 		printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
 		return -ENODEV;
 	}
 
#1 lore raw
Quoted 36 lines · Julia Lawall

From: Julia Lawall <[email protected]>

The initializations of both fcoe_nport_scsi_transport and fcoe_vport_scsi_transport can fail, so test both of them.

The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@r@
identifier x,y,f!={PTR_ERR,ERR_PTR,ERR_CAST};
statement S;
@@

x = f(...);
(
if (\(x == NULL\|IS_ERR(x)\)) S
|
*if (\(y == NULL\|IS_ERR(y)\))
{ ... when != x
return ...; }
)
// </smpl>

---
 drivers/scsi/fcoe/fcoe.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/fcoe/fcoe.c +0 -0
index ba710e3..921b636 100644

drivers/scsi/fcoe/fcoe.c static int __init fcoe_if_init(void) · line 1102

Quoted 8 lines
drivers/scsi/fcoe/fcoe.c +1 -1
@@ -1096,7 +1096,7 @@ static int __init fcoe_if_init(void)
 	fcoe_vport_scsi_transport =
 		fc_attach_transport(&fcoe_vport_fc_functions);
 
-	if (!fcoe_nport_scsi_transport) {
+	if (!fcoe_nport_scsi_transport || !fcoe_vport_scsi_transport) {
 		printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
 		return -ENODEV;
 	}

I only took a quick look, so I may have overlooked something, so bear with me.

fc_attach_transport() allocates memory with kzalloc. If either call fails the other may have succeeded and we'll leak the memory allocated to one of them.

Shouldn't we be kfree()'ing the two variables before the 'return -ENODEV'?

Signature · 3 lines
Jesper Juhl <[email protected]>       http://www.chaosbits.net/
Don't top-post http://www.catb.org/jargon/html/T/top-post.html
Plain text mails only, please.
#2 lore raw
Quoted 49 lines · Jesper Juhl
Quoted 35 lines · Julia Lawall

From: Julia Lawall <[email protected]>

The initializations of both fcoe_nport_scsi_transport and fcoe_vport_scsi_transport can fail, so test both of them.

The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@r@
identifier x,y,f!={PTR_ERR,ERR_PTR,ERR_CAST};
statement S;
@@

x = f(...);
(
if (\(x = NULL\|IS_ERR(x)\)) S
|
*if (\(y = NULL\|IS_ERR(y)\))
{ ... when != x
return ...; }
)
// </smpl>

---
 drivers/scsi/fcoe/fcoe.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/fcoe/fcoe.c +0 -0
index ba710e3..921b636 100644

drivers/scsi/fcoe/fcoe.c static int __init fcoe_if_init(void) · line 1102

Quoted 8 lines
drivers/scsi/fcoe/fcoe.c +1 -1
@@ -1096,7 +1096,7 @@ static int __init fcoe_if_init(void)
 	fcoe_vport_scsi_transport > >  		fc_attach_transport(&fcoe_vport_fc_functions);
 
-	if (!fcoe_nport_scsi_transport) {
+	if (!fcoe_nport_scsi_transport || !fcoe_vport_scsi_transport) {
 		printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
 		return -ENODEV;
 	}
 

I only took a quick look, so I may have overlooked something, so bear with me.

Quoted 5 lines

fc_attach_transport() allocates memory with kzalloc. If either call fails the other may have succeeded and we'll leak the memory allocated to one of them.

Shouldn't we be kfree()'ing the two variables before the 'return -ENODEV'?

It seems reasonable.

julia

#3 lore raw
In reply to Jesper Juhl

From nobody Mon Aug 22 15:57:42 CEST 2011
From: Julia Lawall <[email protected]>
To: Robert Love <[email protected]>
Cc: "James E.J. Bottomley" <[email protected]>,[email protected],[email protected],[email protected]
Subject: [PATCH 2/4] drivers/scsi/fcoe/fcoe.c: add missing test

From: Julia Lawall <[email protected]>

The initializations of both fcoe_nport_scsi_transport and fcoe_vport_scsi_transport can fail, so test both of them.

The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@r@
identifier x,y,f!={PTR_ERR,ERR_PTR,ERR_CAST};
statement S;
@@

x = f(...);
(
if (\(x = NULL\|IS_ERR(x)\)) S
|
*if (\(y = NULL\|IS_ERR(y)\))
{ ... when != x
return ...; }
)
// </smpl>

---
 drivers/scsi/fcoe/fcoe.c |   13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/scsi/fcoe/fcoe.c +11 -2
index ba710e3..9f2e2f4 100644
@@ -1093,11 +1093,20 @@ static int __init fcoe_if_init(void)
 	/* attach to scsi transport */
 	fcoe_nport_scsi_transport  		fc_attach_transport(&fcoe_nport_fc_functions);
+
+	if (!fcoe_nport_scsi_transport) {
+		printk(KERN_ERR
+		       "fcoe_nport: Failed to attach to the FC transport\n");
+		return -ENODEV;
+	}
+
 	fcoe_vport_scsi_transport  		fc_attach_transport(&fcoe_vport_fc_functions);
 
-	if (!fcoe_nport_scsi_transport) {
-		printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
+	if (!fcoe_vport_scsi_transport) {
+		printk(KERN_ERR
+		       "fcoe_vport: Failed to attach to the FC transport\n");
+		fc_release_transport(fcoe_nport_scsi_transport);
 		return -ENODEV;
 	}
 
#4 lore raw
Quoted 53 lines

From nobody Mon Aug 22 15:57:42 CEST 2011
From: Julia Lawall <[email protected]>
To: Robert Love <[email protected]>
Cc: "James E.J. Bottomley" <[email protected]>,devel@open- fcoe.org,[email protected],[email protected]
Subject: [PATCH 2/4] drivers/scsi/fcoe/fcoe.c: add missing test

From: Julia Lawall <[email protected]>

The initializations of both fcoe_nport_scsi_transport and fcoe_vport_scsi_transport can fail, so test both of them.

The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@r@
identifier x,y,f!={PTR_ERR,ERR_PTR,ERR_CAST};
statement S;
@@

x = f(...);
(
if (\(x = NULL\|IS_ERR(x)\)) S
|
*if (\(y = NULL\|IS_ERR(y)\))
{ ... when != x
return ...; }
)
// </smpl>

---
 drivers/scsi/fcoe/fcoe.c |   13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/scsi/fcoe/fcoe.c +7 -0
index ba710e3..9f2e2f4 100644
@@ -1093,11 +1093,20 @@ static int __init fcoe_if_init(void)
 	/* attach to scsi transport */
 	fcoe_nport_scsi_transport >  		fc_attach_transport(&fcoe_nport_fc_functions);
+
+	if (!fcoe_nport_scsi_transport) {
+		printk(KERN_ERR
+		       "fcoe_nport: Failed to attach to the FC transport\n");
+		return -ENODEV;
+	}
+
 	fcoe_vport_scsi_transport >  		fc_attach_transport(&fcoe_vport_fc_functions);
 

drivers/scsi/fcoe/fcoe.c static int __init fcoe_if_init(void) · line 1111

Quoted 10 lines
drivers/scsi/fcoe/fcoe.c +4 -2
-	if (!fcoe_nport_scsi_transport) {
-		printk(KERN_ERR "fcoe: Failed to attach to the FC
 transport\n");
+	if (!fcoe_vport_scsi_transport) {
+		printk(KERN_ERR
+		       "fcoe_vport: Failed to attach to the FC transport\n");
+		fc_release_transport(fcoe_nport_scsi_transport);
 		return -ENODEV;
 	}
 

I understand we were using -ENODEV before, but -ENOMEM is preferred here as that's the only err from fc_attach_transport.

Thanks,
yi

#5 lore raw
Quoted 53 lines · Yi
Quoted 53 lines

From nobody Mon Aug 22 15:57:42 CEST 2011
From: Julia Lawall <[email protected]>
To: Robert Love <[email protected]>
Cc: "James E.J. Bottomley" <[email protected]>,devel@open- fcoe.org,[email protected],[email protected]
Subject: [PATCH 2/4] drivers/scsi/fcoe/fcoe.c: add missing test

From: Julia Lawall <[email protected]>

The initializations of both fcoe_nport_scsi_transport and fcoe_vport_scsi_transport can fail, so test both of them.

The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@r@
identifier x,y,f!={PTR_ERR,ERR_PTR,ERR_CAST};
statement S;
@@

x = f(...);
(
if (\(x = NULL\|IS_ERR(x)\)) S
|
*if (\(y = NULL\|IS_ERR(y)\))
{ ... when != x
return ...; }
)
// </smpl>

---
 drivers/scsi/fcoe/fcoe.c |   13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/scsi/fcoe/fcoe.c +7 -0
index ba710e3..9f2e2f4 100644
@@ -1093,11 +1093,20 @@ static int __init fcoe_if_init(void)
 	/* attach to scsi transport */
 	fcoe_nport_scsi_transport > >  		fc_attach_transport(&fcoe_nport_fc_functions);
+
+	if (!fcoe_nport_scsi_transport) {
+		printk(KERN_ERR
+		       "fcoe_nport: Failed to attach to the FC transport\n");
+		return -ENODEV;
+	}
+
 	fcoe_vport_scsi_transport > >  		fc_attach_transport(&fcoe_vport_fc_functions);
 
Quoted 13 lines

drivers/scsi/fcoe/fcoe.c static int __init fcoe_if_init(void) · line 1110

Quoted 10 lines
drivers/scsi/fcoe/fcoe.c +4 -2
-	if (!fcoe_nport_scsi_transport) {
-		printk(KERN_ERR "fcoe: Failed to attach to the FC
 transport\n");
+	if (!fcoe_vport_scsi_transport) {
+		printk(KERN_ERR
+		       "fcoe_vport: Failed to attach to the FC transport\n");
+		fc_release_transport(fcoe_nport_scsi_transport);
 		return -ENODEV;
 	}
 

I understand we were using -ENODEV before, but -ENOMEM is preferred here as that's the only err from fc_attach_transport.

OK, I'll fix it. Thanks.

julia

#6 lore raw
In reply to Zou, Yi

From: Julia Lawall <[email protected]>

The initializations of both fcoe_nport_scsi_transport and fcoe_vport_scsi_transport can fail, so test both of them.

Convert the return value to -ENOMEM to better reflect the reason for the error.

The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@r@
identifier x,y,f!={PTR_ERR,ERR_PTR,ERR_CAST};
statement S;
@@

x = f(...);
(
if (\(x = NULL\|IS_ERR(x)\)) S
|
*if (\(y = NULL\|IS_ERR(y)\))
{ ... when != x
return ...; }
)
// </smpl>

---
 drivers/scsi/fcoe/fcoe.c |   15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/scsi/fcoe/fcoe.c +12 -3
index ba710e3..2a279eb 100644
@@ -1093,12 +1093,21 @@ static int __init fcoe_if_init(void)
 	/* attach to scsi transport */
 	fcoe_nport_scsi_transport  		fc_attach_transport(&fcoe_nport_fc_functions);
+
+	if (!fcoe_nport_scsi_transport) {
+		printk(KERN_ERR
+		       "fcoe_nport: Failed to attach to the FC transport\n");
+		return -ENOMEM;
+	}
+
 	fcoe_vport_scsi_transport  		fc_attach_transport(&fcoe_vport_fc_functions);
 
-	if (!fcoe_nport_scsi_transport) {
-		printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
-		return -ENODEV;
+	if (!fcoe_vport_scsi_transport) {
+		printk(KERN_ERR
+		       "fcoe_vport: Failed to attach to the FC transport\n");
+		fc_release_transport(fcoe_nport_scsi_transport);
+		return -ENOMEM;
 	}
 
 	return 0;