Skip to contentSkip to conversation

Staging: dgnc: check kzalloc result

PATCH In review Matteo Semenzato 2 participants 2 messages Mar 8, 2015 – Mar 9, 2015 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Staging: dgnc: check kzalloc result

From: Matteo Semenzato <[email protected]>

Return -ENOMEM if allocating brd->flipbuf fails.

---
 drivers/staging/dgnc/dgnc_driver.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/staging/dgnc/dgnc_driver.c +4 -0
index f177d3a..711af02 100644
@@ -622,6 +622,10 @@ static int dgnc_found_board(struct pci_dev *pdev, int id)
 	 * context, and there are no locks held.
 	 */
 	brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
+	if (!brd->flipbuf) {
+		kfree(brd);
+		return -ENOMEM;
+	}
 
 	wake_up_interruptible(&brd->state_wait);
 
Signature · 1 line
2.3.1
#1 lore raw
Quoted 14 lines · Matteo Semenzato

From: Matteo Semenzato <[email protected]>

Return -ENOMEM if allocating brd->flipbuf fails.

---
 drivers/staging/dgnc/dgnc_driver.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/staging/dgnc/dgnc_driver.c +0 -0
index f177d3a..711af02 100644

drivers/staging/dgnc/dgnc_driver.c static int dgnc_found_board(struct pci_dev *pdev, int id) · line 627

Quoted 6 lines
drivers/staging/dgnc/dgnc_driver.c +3 -0
@@ -622,6 +622,10 @@ static int dgnc_found_board(struct pci_dev *pdev, int id)
 	 * context, and there are no locks held.
 	 */
 	brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
+	if (!brd->flipbuf) {
+		kfree(brd);
+		return -ENOMEM;

This leaves a freed pointer in "dgnc_Board[dgnc_NumBoards]" which is sort of ugly. It doesn't free "brd->msgbuf_head" which is a bug.

Please read my essay on error hanling:
https://plus.google.com/106378716002406849458

What we want is :

	ret = do_one();
	if (ret)
		return ret;
	ret = do_two();
	if (ret)
		goto undo_one;

	ret = do_three();
	if (ret)
		goto undo_two;

	return 0;

err_undo_two:

	undo_two();
err_undo_one:
	undo_one();

	return ret;

Basically, a list of commands and a mirror list of undos. This is standard kernel style and the error handling basically writes itself automatically.

regards,
dan carpenter

Quoted 11 lines
drivers/staging/dgnc/dgnc_driver.c +1 -0
+	}
 
 	wake_up_interruptible(&brd->state_wait);
 
Signature · 6 lines
2.3.1

_______________________________________________
devel mailing list
[email protected]
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel