mantis: Fix goto labels
Subsystems (2)
cover mantis: Fix goto labels
I found two issues regarding goto labels in the mantis driver when checking a smatch warning and addressed them in two separate patches. Please be aware that these patches have only been compile-tested since I do not have access to the corresponding hardware.
Silvan Jegen (2):
[media] mantis: Move jump label to activate dead code [media] mantis: Use correct goto labels for cleanup on error
drivers/media/pci/mantis/mantis_cards.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.2.2
PATCH 1/2 mantis: Move jump label to activate dead code
Due to a misplaced goto label mantis_uart_exit is never called. Adjusting the label position (while correcting its numbering) changes this.
This issue was found using the smatch static checker.
--- drivers/media/pci/mantis/mantis_cards.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/media/pci/mantis/mantis_cards.c +3 -2
@@ -215,10 +215,11 @@ static int mantis_pci_probe(struct pci_dev *pdev,dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DVB initialization failed <%d>" , err); goto fail4; } + err =mantis_uart_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART initialization failed <%d>" , err); - goto fail6; + goto fail5; } devs++;
@@ -226,10 +227,10 @@ static int mantis_pci_probe(struct pci_dev *pdev,return err; +fail5:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART exit! <%d>" , err);mantis_uart_exit (mantis); -fail6: fail4:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA exit! <%d>" , err);mantis_dma_exit (mantis);
Signature · 1 line
2.2.2
Re: [PATCH 1/2] [media] mantis: Move jump label to activate dead code
Quoted 19 lines · Silvan Jegen
drivers/media/pci/mantis/mantis_cards.c +2
-1@@ -215,10 +215,11 @@ static int mantis_pci_probe(struct pci_dev *pdev,dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DVB initialization failed <%d>" , err); goto fail4; } + err =mantis_uart_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART initialization failed <%d>" , err); - goto fail6; + goto fail5; } devs++;@@ -226,10 +227,10 @@ static int mantis_pci_probe(struct pci_dev *pdev,return err;
drivers/media/pci/mantis/mantis_cards.c static int mantis_pci_probe(struct pci_dev *pdev, · line 236
Quoted 10 lines
drivers/media/pci/mantis/mantis_cards.c +1
-1+fail5:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART exit! <%d>" , err);mantis_uart_exit (mantis); -fail6: fail4:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA exit! <%d>" , err);mantis_dma_exit (mantis);
This patch isn't right, I'm afraid. The person who wrote this driver deliberately added some dead error handling code in case we change it later and need to activate it. It's an ugly thing to do because it causes static checker warnings, and confusion, and, in real life, then we are not ever going to need to activate it. It's defensive programming but we don't do defensive programming.
http://lwn.net/Articles/604813/ Just delete this dead code.
Also this code uses GW-BASIC style numbered gotos. So ugly! The label names should be based on what the label location does. Eg "err_uart_exit", "err_dma_exit". I have written an essay about label
names: https://plus.google.com/106378716002406849458/posts/dnanfhQ4mHQ
In theory, we should be calling mantis_dvb_exit() if mantis_uart_init() fails. In reality, it can't fail but it's still wrong to leave that out.
These patches would be easier to review if you just folded them into one patch. Call it "fix error error handling" or something.
regards,
dan carpenter
Re: [PATCH 1/2] [media] mantis: Move jump label to activate dead code
Thanks for the review, Dan!
Quoted 26 lines · Dan Carpenter
Quoted 25 lines · Silvan Jegen
drivers/media/pci/mantis/mantis_cards.c +3
-1@@ -215,10 +215,11 @@ static int mantis_pci_probe(struct pci_dev *pdev,dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DVB initialization failed <%d>" , err); goto fail4; } + err =mantis_uart_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART initialization failed <%d>" , err); - goto fail6; + goto fail5; } devs++;@@ -226,10 +227,10 @@ static int mantis_pci_probe(struct pci_dev *pdev,return err; +fail5:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART exit! <%d>" , err);mantis_uart_exit (mantis);
Quoted 12 lines
drivers/media/pci/mantis/mantis_cards.c static int mantis_pci_probe(struct pci_dev *pdev, · line 236
Quoted 4 lines
drivers/media/pci/mantis/mantis_cards.c +0
-1-fail6: fail4:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA exit! <%d>" , err);mantis_dma_exit (mantis);This patch isn't right, I'm afraid. The person who wrote this driver deliberately added some dead error handling code in case we change it later and need to activate it. It's an ugly thing to do because it causes static checker warnings, and confusion, and, in real life, then we are not ever going to need to activate it. It's defensive programming but we don't do defensive programming.
http://lwn.net/Articles/604813/ Just delete this dead code.
I will do that.
Quoted 8 lines
Also this code uses GW-BASIC style numbered gotos. So ugly! The label names should be based on what the label location does. Eg "
err_uart_exit", "err_dma_exit". I have written an essay about label
names: https://plus.google.com/106378716002406849458/posts/dnanfhQ4mHQIn theory, we should be calling
mantis_dvb_exit()ifmantis_uart_init()fails. In reality, it can't fail but it's still wrong to leave that out.
In the next version of the patch, I will change the names of the goto labels accordingly. I will add the mantis_dvb_exit() call as well.
Quoted 2 lines
These patches would be easier to review if you just folded them into one patch. Call it "fix error error handling" or something.
Ok, I will fold the second patch into the first one replacing the goto label names with more appropriate ones. In the first patch I will just delete the dead code and change the jump labels to be more descriptive.
I will leave for holidays at the end of the week so it's probably better if I wait with sending the new version of the patch until I am back.
Thanks again,
Silvan
PATCH v2 1/1 mantis: fix error handling
In reply to Dan CarpenterRemove dead code, make goto label names more expressive and add a label in order to call mantis_dvb_exit if mantis_uart_init fails.
Also make sure that mantis_pci_exit is called if we fail the mantis_stream_control call and that we call mantis_i2c_exit if mantis_get_mac fails.
---
V2 Changes (due to Dan Carpenter's review):
- Remove dead code, do not activate it
- Make goto labels more expressive
- Add a call to mantis_dvb_exit
drivers/media/pci/mantis/mantis_cards.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-)
drivers/media/pci/mantis/mantis_cards.c +16 -17
@@ -170,7 +170,7 @@ static int mantis_pci_probe(struct pci_dev *pdev,if (mantis =NULL ) {printk (KERN_ERR "%s ERROR: Out of memory\n" , __func__); err = -ENOMEM ; - goto fail0; +return err; } mantis->num = devs;
@@ -183,70 +183,69 @@ static int mantis_pci_probe(struct pci_dev *pdev, err =mantis_pci_init (mantis);if (err) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis PCI initialization failed <%d>" , err); - goto fail1; + goto err_free_mantis; } err =mantis_stream_control (mantis,STREAM_TO_HIF );if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis stream control failed <%d>" , err); - goto fail1; + goto err_pci_exit; } err =mantis_i2c_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis I2C initialization failed <%d>" , err); - goto fail2; + goto err_pci_exit; } err =mantis_get_mac (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis MAC address read failed <%d>" , err); - goto fail2; + goto err_i2c_exit; } err =mantis_dma_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA initialization failed <%d>" , err); - goto fail3; + goto err_i2c_exit; } err =mantis_dvb_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DVB initialization failed <%d>" , err); - goto fail4; + goto err_dma_exit; } + err =mantis_uart_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART initialization failed <%d>" , err); - goto fail6; + goto err_dvb_exit; } devs++;return err; +err_dvb_exit: +dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DVB exit! <%d>" , err); +mantis_dvb_exit (mantis); -dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART exit! <%d>" , err); -mantis_uart_exit (mantis); - -fail6: -fail4: +err_dma_exit:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA exit! <%d>" , err);mantis_dma_exit (mantis); -fail3: +err_i2c_exit:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis I2C exit! <%d>" , err);mantis_i2c_exit (mantis); -fail2: +err_pci_exit:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis PCI exit! <%d>" , err);mantis_pci_exit (mantis); -fail1: +err_free_mantis:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis free! <%d>" , err);kfree (mantis); -fail0:return err; }
Signature · 1 line
2.3.3
Re: [PATCH v2] [media] mantis: fix error handling
Am 22.03.2015 18:16, schrieb Silvan Jegen:
Quoted 74 lines
Remove dead code, make goto label names more expressive and add a label in order to call
mantis_dvb_exitifmantis_uart_initfails.Also make sure that
mantis_pci_exitis called if we fail themantis_stream_controlcall and that we callmantis_i2c_exitifmantis_get_macfails.Signed-off-by Silvan Jegen <[email protected]>---
V2 Changes (due to Dan Carpenter's review):
- Remove dead code, do not activate it
- Make goto labels more expressive
- Add a call tomantis_dvb_exitdrivers/media/pci/mantis/mantis_cards.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-)drivers/media/pci/mantis/mantis_cards.c +8
-7@@ -170,7 +170,7 @@ static int mantis_pci_probe(struct pci_dev *pdev,if (mantis =NULL ) {printk (KERN_ERR "%s ERROR: Out of memory\n" , __func__); err = -ENOMEM ; - goto fail0; +return err; } mantis->num = devs;@@ -183,70 +183,69 @@ static int mantis_pci_probe(struct pci_dev *pdev, err =mantis_pci_init (mantis);if (err) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis PCI initialization failed <%d>" , err); - goto fail1; + goto err_free_mantis; } err =mantis_stream_control (mantis,STREAM_TO_HIF );if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis stream control failed <%d>" , err); - goto fail1; + goto err_pci_exit; } err =mantis_i2c_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis I2C initialization failed <%d>" , err); - goto fail2; + goto err_pci_exit; } err =mantis_get_mac (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis MAC address read failed <%d>" , err); - goto fail2; + goto err_i2c_exit; } err =mantis_dma_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA initialization failed <%d>" , err); - goto fail3; + goto err_i2c_exit; } err =mantis_dvb_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DVB initialization failed <%d>" , err); - goto fail4; + goto err_dma_exit; } + err =mantis_uart_init (mantis);
drivers/media/pci/mantis/mantis_cards.c static int mantis_pci_probe(struct pci_dev *pdev, · line 228
Quoted 10 lines
drivers/media/pci/mantis/mantis_cards.c +1
-1if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART initialization failed <%d>" , err); - goto fail6; + goto err_dvb_exit; } devs++;return err;
Hi Silvan Jegen,
i found the dprintk() a bit confusing, if i understand the code correctly it will always print on error:
the dprintf found in if (err<0) like :
dprintk(MANTIS_ERROR, 1, "ERROR: Mantis DVB initialization failed <%d>", err);
and then
dprintk(MANTIS_ERROR, 1, "ERROR: Mantis DVB exit! <%d>", err);
maybe this is more a question for the maintainer, but this seems a bit useless.
re,
wh
Quoted 32 lines
drivers/media/pci/mantis/mantis_cards.c +7
-9+err_dvb_exit: +dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DVB exit! <%d>" , err); +mantis_dvb_exit (mantis); -dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART exit! <%d>" , err); -mantis_uart_exit (mantis); - -fail6: -fail4: +err_dma_exit:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA exit! <%d>" , err);mantis_dma_exit (mantis); -fail3: +err_i2c_exit:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis I2C exit! <%d>" , err);mantis_i2c_exit (mantis); -fail2: +err_pci_exit:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis PCI exit! <%d>" , err);mantis_pci_exit (mantis); -fail1: +err_free_mantis:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis free! <%d>" , err);kfree (mantis); -fail0:return err; }
Re: [PATCH v2] [media] mantis: fix error handling
In reply to Silvan JegenGood, but still a couple nits.
drivers/media/pci/mantis/mantis_cards.c static int mantis_pci_probe(struct pci_dev *pdev, · line 173
Quoted 8 lines · Silvan Jegen
drivers/media/pci/mantis/mantis_cards.c +1
-1@@ -170,7 +170,7 @@ static int mantis_pci_probe(struct pci_dev *pdev,if (mantis ==NULL ) {printk (KERN_ERR "%s ERROR: Out of memory\n" , __func__); err = -ENOMEM ; - goto fail0; +return err;
Just:
return -ENOMEM;
Quoted 46 lines
drivers/media/pci/mantis/mantis_cards.c +7
-6} mantis->num = devs;@@ -183,70 +183,69 @@ static int mantis_pci_probe(struct pci_dev *pdev, err =mantis_pci_init (mantis);if (err) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis PCI initialization failed <%d>" , err); - goto fail1; + goto err_free_mantis; } err =mantis_stream_control (mantis,STREAM_TO_HIF );if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis stream control failed <%d>" , err); - goto fail1; + goto err_pci_exit; } err =mantis_i2c_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis I2C initialization failed <%d>" , err); - goto fail2; + goto err_pci_exit; } err =mantis_get_mac (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis MAC address read failed <%d>" , err); - goto fail2; + goto err_i2c_exit; } err =mantis_dma_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA initialization failed <%d>" , err); - goto fail3; + goto err_i2c_exit; } err =mantis_dvb_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DVB initialization failed <%d>" , err); - goto fail4; + goto err_dma_exit; } +
drivers/media/pci/mantis/mantis_cards.c static int mantis_pci_probe(struct pci_dev *pdev, · line 227
Quoted 10 lines
drivers/media/pci/mantis/mantis_cards.c +1
-1err =mantis_uart_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART initialization failed <%d>" , err); - goto fail6; + goto err_dvb_exit; } devs++;return err;
return 0;
That was in the original, but let's just clean it up as well.
Quoted 18 lines
drivers/media/pci/mantis/mantis_cards.c +5
-6+err_dvb_exit: +dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DVB exit! <%d>" , err); +mantis_dvb_exit (mantis); -dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART exit! <%d>" , err); -mantis_uart_exit (mantis); - -fail6: -fail4: +err_dma_exit:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA exit! <%d>" , err);mantis_dma_exit (mantis); -fail3: +err_i2c_exit:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis I2C exit! <%d>" , err);mantis_i2c_exit (mantis);
drivers/media/pci/mantis/mantis_cards.c static int mantis_pci_probe(struct pci_dev *pdev, · line 247
Quoted 10 lines
drivers/media/pci/mantis/mantis_cards.c +2
-2-fail2: +err_pci_exit:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis PCI exit! <%d>" , err);mantis_pci_exit (mantis); -fail1: +err_free_mantis:dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis free! <%d>" , err);kfree (mantis);
Remove all these dprintks in the error handling.
regards,
dan carpenter
PATCH v3 1/1 mantis: fix error handling
Remove dead code, make goto label names more expressive and add a label in order to call mantis_dvb_exit if mantis_uart_init fails.
Also make sure that mantis_pci_exit is called if we fail the mantis_stream_control call and that we call mantis_i2c_exit if mantis_get_mac fails.
---
V3 Changes (due to Dan Carpenter's and Walter Harms' reviews):
- return -ENOMEM/0 directly
- remove dprintk calls in the error handling part
V2 Changes (due to Dan Carpenter's review):
- Remove dead code, do not activate it
- Make goto labels more expressive
- Add a call to mantis_dvb_exit
drivers/media/pci/mantis/mantis_cards.c | 39 ++++++++++++++------------------- 1 file changed, 16 insertions(+), 23 deletions(-)
drivers/media/pci/mantis/mantis_cards.c +16 -23
@@ -169,8 +169,7 @@ static int mantis_pci_probe(struct pci_dev *pdev, mantis =kzalloc (sizeof (struct mantis_pci ),GFP_KERNEL );if (mantis =NULL ) {printk (KERN_ERR "%s ERROR: Out of memory\n" , __func__); - err = -ENOMEM ; - goto fail0; +return -ENOMEM ; } mantis->num = devs;
@@ -183,70 +182,64 @@ static int mantis_pci_probe(struct pci_dev *pdev, err =mantis_pci_init (mantis);if (err) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis PCI initialization failed <%d>" , err); - goto fail1; + goto err_free_mantis; } err =mantis_stream_control (mantis,STREAM_TO_HIF );if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis stream control failed <%d>" , err); - goto fail1; + goto err_pci_exit; } err =mantis_i2c_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis I2C initialization failed <%d>" , err); - goto fail2; + goto err_pci_exit; } err =mantis_get_mac (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis MAC address read failed <%d>" , err); - goto fail2; + goto err_i2c_exit; } err =mantis_dma_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA initialization failed <%d>" , err); - goto fail3; + goto err_i2c_exit; } err =mantis_dvb_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DVB initialization failed <%d>" , err); - goto fail4; + goto err_dma_exit; } + err =mantis_uart_init (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART initialization failed <%d>" , err); - goto fail6; + goto err_dvb_exit; } devs++; -return err; - +return 0 ; -dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis UART exit! <%d>" , err); -mantis_uart_exit (mantis); +err_dvb_exit: +mantis_dvb_exit (mantis); -fail6: -fail4: -dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis DMA exit! <%d>" , err); +err_dma_exit:mantis_dma_exit (mantis); -fail3: -dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis I2C exit! <%d>" , err); +err_i2c_exit:mantis_i2c_exit (mantis); -fail2: -dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis PCI exit! <%d>" , err); +err_pci_exit:mantis_pci_exit (mantis); -fail1: -dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis free! <%d>" , err); +err_free_mantis:kfree (mantis); -fail0:return err; }
Signature · 1 line
2.3.3
Re: [PATCH V3] [media] mantis: fix error handling
Quoted 8 lines · Silvan Jegen
Remove dead code, make goto label names more expressive and add a label in order to call
mantis_dvb_exitifmantis_uart_initfails.Also make sure that
mantis_pci_exitis called if we fail themantis_stream_controlcall and that we callmantis_i2c_exitifmantis_get_macfails.Signed-off-by Silvan Jegen <[email protected]>
Looks great. Thanks!
regards,
dan carpenter
PATCH 2/2 mantis: Use correct goto labels for cleanup on error
In reply to Silvan JegenAfter calling mantis_pci_init we have to jump to fail2 in order to call the corresponding mantis_pci_exit. Similarly, after calling mantis_get_mac we have already called mantis_pci_init and mantis_i2c_init so we need to jump to fail3 if we want to call the corresponding exit functions.
--- drivers/media/pci/mantis/mantis_cards.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/pci/mantis/mantis_cards.c +2 -2
@@ -183,7 +183,7 @@ static int mantis_pci_probe(struct pci_dev *pdev, err =mantis_pci_init (mantis);if (err) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis PCI initialization failed <%d>" , err); - goto fail1; + goto fail2; } err =mantis_stream_control (mantis,STREAM_TO_HIF );
@@ -201,7 +201,7 @@ static int mantis_pci_probe(struct pci_dev *pdev, err =mantis_get_mac (mantis);if (err <0 ) {dprintk (MANTIS_ERROR ,1 ,"ERROR: Mantis MAC address read failed <%d>" , err); - goto fail2; + goto fail3; } err =mantis_dma_init (mantis);
Signature · 1 line
2.2.2