staging: gpib: cb7210: Add error handling
PATCH v2 1/1 staging: gpib: cb7210: Add error handling
The if condition in cb7210_init_module() would always fail as the value of err is constantly 0. The function cb_pcmcia_init_module() can be refactored into a single line, and can be replaced entirely with pcmcia_register_driver().
Replace redundant cb_pcmcia_init_module() with pcmcia_register_driver() which returns appropriate error code if it fails. Handle the error if it fails and print the debug message.
This issue was reported by Coverity Scan.
Report:
CID 1635894: (#1 of 1): 'Constant' variable guards dead code (DEADCODE)dead_error_line: Execution cannot reach this statement: return -1;.
---
v1 --> v2: Replaced the redundant cb_pcmcia_init_module() with
pcmcia_register_driver().
drivers/staging/gpib/cb7210/cb7210.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/staging/gpib/cb7210/cb7210.c +4 -9
@@ -1351,12 +1351,6 @@ static struct pcmcia_driver cb_gpib_cs_driver = { .resume = cb_gpib_resume, }; -int cb_pcmcia_init_module (void ) -{ -pcmcia_register_driver (&cb_gpib_cs_driver); -return 0 ; -} -void cb_pcmcia_cleanup_module (void ) {DEBUG (0 ,"cb_gpib_cs: unloading\n" );
@@ -1526,11 +1520,12 @@ static int __init cb7210_init_module(void)gpib_register_driver (&cb_pcmcia_interface,THIS_MODULE );gpib_register_driver (&cb_pcmcia_accel_interface,THIS_MODULE );gpib_register_driver (&cb_pcmcia_unaccel_interface,THIS_MODULE ); - err +=cb_pcmcia_init_module (); + err =pcmcia_register_driver (&cb_gpib_cs_driver);#endif -if (err) +if (err) { +pr_err ("cb7210: registering PCMCIA driver with the bus core failed\n" );return -1 ; - + }return 0 ; }
Signature · 1 line
2.34.1
Greg KH
maintainerQuoted 40 lines · Nihar Chaithanya
The if condition in
cb7210_init_module()would always fail as the value of err is constantly 0. The functioncb_pcmcia_init_module()can be refactored into a single line, and can be replaced entirely withpcmcia_register_driver().Replace redundant
cb_pcmcia_init_module()withpcmcia_register_driver()which returns appropriate error code if it fails. Handle the error if it fails and print the debug message.This issue was reported by Coverity Scan.
Report:
CID 1635894: (#1 of 1): 'Constant' variable guards dead code (DEADCODE)dead_error_line: Execution cannot reach this statement: return -1;.Signed-off-by Nihar Chaithanya <[email protected]>---
v1 --> v2: Replaced the redundantcb_pcmcia_init_module()with
pcmcia_register_driver().drivers/staging/gpib/cb7210/cb7210.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)drivers/staging/gpib/cb7210/cb7210.c +0
-6@@ -1351,12 +1351,6 @@ static struct pcmcia_driver cb_gpib_cs_driver = { .resume = cb_gpib_resume, }; -int cb_pcmcia_init_module (void ) -{ -pcmcia_register_driver (&cb_gpib_cs_driver); -return 0 ; -} -void cb_pcmcia_cleanup_module (void ) {DEBUG (0 ,"cb_gpib_cs: unloading\n" );
drivers/staging/gpib/cb7210/cb7210.c static int __init cb7210_init_module(void) · line 1527
Quoted 10 lines
drivers/staging/gpib/cb7210/cb7210.c +3
-2@@ -1526,11 +1520,12 @@ static int __init cb7210_init_module(void)gpib_register_driver (&cb_pcmcia_interface,THIS_MODULE );gpib_register_driver (&cb_pcmcia_accel_interface,THIS_MODULE );gpib_register_driver (&cb_pcmcia_unaccel_interface,THIS_MODULE ); - err +=cb_pcmcia_init_module (); + err =pcmcia_register_driver (&cb_gpib_cs_driver);#endif -if (err) +if (err) { +pr_err ("cb7210: registering PCMCIA driver with the bus core failed\n" );return -1 ;
Not your issue, but "-1" is not a valid error number, shouldn't this be returning a real error (i.e. the one that was returned?)
And err += is crazy, thanks for removing that, BUT you forgot to unwind the previous functions so now if pcmcia_register_driver() fails, you have a semi-registered system and then the module will unload (as init fails) and bad things will happen :(
So you kind of just made the code worse overall, not better, which really isn't a good idea for a "bugfix" that covertity was trying to point out.
thanks,
greg k-h
Greg KH
maintainerQuoted 55 lines · Greg KH
Quoted 40 lines · Nihar Chaithanya
The if condition in
cb7210_init_module()would always fail as the value of err is constantly 0. The functioncb_pcmcia_init_module()can be refactored into a single line, and can be replaced entirely withpcmcia_register_driver().Replace redundant
cb_pcmcia_init_module()withpcmcia_register_driver()which returns appropriate error code if it fails. Handle the error if it fails and print the debug message.This issue was reported by Coverity Scan.
Report:
CID 1635894: (#1 of 1): 'Constant' variable guards dead code (DEADCODE)dead_error_line: Execution cannot reach this statement: return -1;.Signed-off-by Nihar Chaithanya <[email protected]>---
v1 --> v2: Replaced the redundantcb_pcmcia_init_module()with
pcmcia_register_driver().drivers/staging/gpib/cb7210/cb7210.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)drivers/staging/gpib/cb7210/cb7210.c +0
-6@@ -1351,12 +1351,6 @@ static struct pcmcia_driver cb_gpib_cs_driver = { .resume = cb_gpib_resume, }; -int cb_pcmcia_init_module (void ) -{ -pcmcia_register_driver (&cb_gpib_cs_driver); -return 0 ; -} -void cb_pcmcia_cleanup_module (void ) {DEBUG (0 ,"cb_gpib_cs: unloading\n" );drivers/staging/gpib/cb7210/cb7210.c static int __init cb7210_init_module(void) · line 1527
Quoted 10 lines
drivers/staging/gpib/cb7210/cb7210.c +3
-2@@ -1526,11 +1520,12 @@ static int __init cb7210_init_module(void)gpib_register_driver (&cb_pcmcia_interface,THIS_MODULE );gpib_register_driver (&cb_pcmcia_accel_interface,THIS_MODULE );gpib_register_driver (&cb_pcmcia_unaccel_interface,THIS_MODULE ); - err +=cb_pcmcia_init_module (); + err =pcmcia_register_driver (&cb_gpib_cs_driver);#endif -if (err) +if (err) { +pr_err ("cb7210: registering PCMCIA driver with the bus core failed\n" );return -1 ;Not your issue, but "-1" is not a valid error number, shouldn't this be returning a real error (i.e. the one that was returned?)
Quoted 8 lines
And err += is crazy, thanks for removing that, BUT you forgot to unwind the previous functions so now if
pcmcia_register_driver()fails, you have a semi-registered system and then the module will unload (as init fails) and bad things will happen :(So you kind of just made the code worse overall, not better, which really isn't a good idea for a "bugfix" that covertity was trying to point out.
And (this is taking you down a path that is much larger than you expected to just fix this one issue, sorry), it looks like gpib_register_driver() can fail, so it MUST return an error value so you can properly unwind from it. So that too needs to be fixed up here (also minor nit, gpib_register_driver() should not be calling pr_info(), but that's a different issue.)
In all, there are loads of tiny things like this to fix up in the gpib code, which is great, have fun!
thanks,
greg k-h
Quoted 55 lines · Greg KH
Quoted 54 lines · Greg KH
Quoted 40 lines · Nihar Chaithanya
The if condition in
cb7210_init_module()would always fail as the value of err is constantly 0. The functioncb_pcmcia_init_module()can be refactored into a single line, and can be replaced entirely withpcmcia_register_driver().Replace redundant
cb_pcmcia_init_module()withpcmcia_register_driver()which returns appropriate error code if it fails. Handle the error if it fails and print the debug message.This issue was reported by Coverity Scan.
Report:
CID 1635894: (#1 of 1): 'Constant' variable guards dead code (DEADCODE)dead_error_line: Execution cannot reach this statement: return -1;.Signed-off-by Nihar Chaithanya <[email protected]>---
v1 --> v2: Replaced the redundantcb_pcmcia_init_module()with
pcmcia_register_driver().drivers/staging/gpib/cb7210/cb7210.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)drivers/staging/gpib/cb7210/cb7210.c +0
-6@@ -1351,12 +1351,6 @@ static struct pcmcia_driver cb_gpib_cs_driver = { .resume = cb_gpib_resume, }; -int cb_pcmcia_init_module (void ) -{ -pcmcia_register_driver (&cb_gpib_cs_driver); -return 0 ; -} -void cb_pcmcia_cleanup_module (void ) {DEBUG (0 ,"cb_gpib_cs: unloading\n" );drivers/staging/gpib/cb7210/cb7210.c static int __init cb7210_init_module(void) · line 1527
Quoted 10 lines
drivers/staging/gpib/cb7210/cb7210.c +3
-2@@ -1526,11 +1520,12 @@ static int __init cb7210_init_module(void)gpib_register_driver (&cb_pcmcia_interface,THIS_MODULE );gpib_register_driver (&cb_pcmcia_accel_interface,THIS_MODULE );gpib_register_driver (&cb_pcmcia_unaccel_interface,THIS_MODULE ); - err +=cb_pcmcia_init_module (); + err =pcmcia_register_driver (&cb_gpib_cs_driver);#endif -if (err) +if (err) { +pr_err ("cb7210: registering PCMCIA driver with the bus core failed\n" );return -1 ;Not your issue, but "-1" is not a valid error number, shouldn't this be returning a real error (i.e. the one that was returned?)
Quoted 4 lines
Quoted 4 lines
And err += is crazy, thanks for removing that, BUT you forgot to unwind the previous functions so now if
pcmcia_register_driver()fails, you have a semi-registered system and then the module will unload (as init fails) and bad things will happen :(
So I need be unregistering the driver if the pcmcia_register_driver() fails right?
Quoted 9 lines
Quoted 3 lines
So you kind of just made the code worse overall, not better, which really isn't a good idea for a "bugfix" that covertity was trying to point out.
And (this is taking you down a path that is much larger than you expected to just fix this one issue, sorry), it looks like
gpib_register_driver()can fail, so it MUST return an error value so you can properly unwind from it. So that too needs to be fixed up here (also minor nit,gpib_register_driver()should not be callingpr_info(), but that's a different issue.)
Yes Greg, gpib_register_driver() should be returning -ENOMEM if it fails, thank you for pointing it out, I'll change the gpib code wherever the drivers are registered, I'll send the patch as soon as I can.
Quoted 2 lines
In all, there are loads of tiny things like this to fix up in the gpib code, which is great, have fun!
yes, I am happy to contribute.
Quoted 3 lines
thanks,
greg k-h
thank you,
Nihar
Greg KH
maintainerQuoted 56 lines · Nihar Chaithanya
Quoted 54 lines · Greg KH
Quoted 53 lines · Greg KH
Quoted 39 lines · Nihar Chaithanya
The if condition in
cb7210_init_module()would always fail as the value of err is constantly 0. The functioncb_pcmcia_init_module()can be refactored into a single line, and can be replaced entirely withpcmcia_register_driver().Replace redundant
cb_pcmcia_init_module()withpcmcia_register_driver()which returns appropriate error code if it fails. Handle the error if it fails and print the debug message.This issue was reported by Coverity Scan.
Report:
CID 1635894: (#1 of 1): 'Constant' variable guards dead code (DEADCODE)dead_error_line: Execution cannot reach this statement: return -1;.Signed-off-by Nihar Chaithanya <[email protected]>---
v1 --> v2: Replaced the redundantcb_pcmcia_init_module()with
pcmcia_register_driver().drivers/staging/gpib/cb7210/cb7210.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)drivers/staging/gpib/cb7210/cb7210.c +0
-6@@ -1351,12 +1351,6 @@ static struct pcmcia_driver cb_gpib_cs_driver = { .resume = cb_gpib_resume, }; -int cb_pcmcia_init_module (void ) -{ -pcmcia_register_driver (&cb_gpib_cs_driver); -return 0 ; -} -void cb_pcmcia_cleanup_module (void ) {DEBUG (0 ,"cb_gpib_cs: unloading\n" );drivers/staging/gpib/cb7210/cb7210.c static int __init cb7210_init_module(void) · line 1527
Quoted 10 lines
drivers/staging/gpib/cb7210/cb7210.c +3
-2@@ -1526,11 +1520,12 @@ static int __init cb7210_init_module(void)gpib_register_driver (&cb_pcmcia_interface,THIS_MODULE );gpib_register_driver (&cb_pcmcia_accel_interface,THIS_MODULE );gpib_register_driver (&cb_pcmcia_unaccel_interface,THIS_MODULE ); - err +=cb_pcmcia_init_module (); + err =pcmcia_register_driver (&cb_gpib_cs_driver);#endif -if (err) +if (err) { +pr_err ("cb7210: registering PCMCIA driver with the bus core failed\n" );return -1 ;Not your issue, but "-1" is not a valid error number, shouldn't this be returning a real error (i.e. the one that was returned?)
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
And err += is crazy, thanks for removing that, BUT you forgot to unwind the previous functions so now if
pcmcia_register_driver()fails, you have a semi-registered system and then the module will unload (as init fails) and bad things will happen :(So I need be unregistering the driver if the
pcmcia_register_driver()fails right?
Yes, all of them.
thanks,
greg k-h