[PATCH v4 1/2] staging: dgap: tty.c: adds error handing in tty driver allocations

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch adds error handling to the
tty_driver allocations in dgap_tty_register.

Now the code handles the possibility of an
alloc_tty_driver, a tty_register_driver, and
a brd->SerialDriver->ttys or brd->PrintDriver->ttys
allocation failure.

Signed-off-by: Lidza Louina <lidza.louina@xxxxxxxxx>
---
 drivers/staging/dgap/dgap_tty.c | 39 ++++++++++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/drivers/staging/dgap/dgap_tty.c b/drivers/staging/dgap/dgap_tty.c
index 924e2bf..015bccf 100644
--- a/drivers/staging/dgap/dgap_tty.c
+++ b/drivers/staging/dgap/dgap_tty.c
@@ -220,6 +220,8 @@ int dgap_tty_register(struct board_t *brd)
 	DPR_INIT(("tty_register start"));
 
 	brd->SerialDriver = alloc_tty_driver(MAXPORTS);
+	if (!brd->SerialDriver)
+		return -ENOMEM;
 
 	snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgap_%d_", brd->boardnum);
 	brd->SerialDriver->name = brd->SerialName;
@@ -234,9 +236,10 @@ int dgap_tty_register(struct board_t *brd)
 
 	/* The kernel wants space to store pointers to tty_structs */
 	brd->SerialDriver->ttys = dgap_driver_kzmalloc(MAXPORTS * sizeof(struct tty_struct *), GFP_KERNEL);
-	if (!brd->SerialDriver->ttys)
-		return(-ENOMEM);
-
+	if (!brd->SerialDriver->ttys){
+		rc = -ENOMEM;
+		goto err_put_tty_serial;
+	}
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
 	brd->SerialDriver->refcount = brd->TtyRefCnt;
 #endif
@@ -253,7 +256,10 @@ int dgap_tty_register(struct board_t *brd)
 	 * we are when we get into the dgap_tty_open() routine.
 	 */
 	brd->PrintDriver = alloc_tty_driver(MAXPORTS);
-
+	if (!brd->PrintDriver) {
+		rc = -ENOMEM;
+		goto err_free_serial_ttys;
+	}
 	snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgap_%d_", brd->boardnum);
 	brd->PrintDriver->name = brd->PrintName;
 	brd->PrintDriver->name_base = 0;
@@ -267,8 +273,10 @@ int dgap_tty_register(struct board_t *brd)
 
 	/* The kernel wants space to store pointers to tty_structs */
 	brd->PrintDriver->ttys = dgap_driver_kzmalloc(MAXPORTS * sizeof(struct tty_struct *), GFP_KERNEL);
-	if (!brd->PrintDriver->ttys)
-		return(-ENOMEM);
+	if (!brd->PrintDriver->ttys){
+		rc = -ENOMEM;
+		goto err_put_tty_print;
+	}
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
 	brd->PrintDriver->refcount = brd->TtyRefCnt;
@@ -285,7 +293,8 @@ int dgap_tty_register(struct board_t *brd)
 		rc = tty_register_driver(brd->SerialDriver);
 		if (rc < 0) {
 			APR(("Can't register tty device (%d)\n", rc));
-			return(rc);
+			goto err_put_tty_serial;
+			goto err_free_print_ttys;
 		}
 		brd->dgap_Major_Serial_Registered = TRUE;
 		dgap_BoardsByMajor[brd->SerialDriver->major] = brd;
@@ -297,7 +306,8 @@ int dgap_tty_register(struct board_t *brd)
  		rc = tty_register_driver(brd->PrintDriver);
 		if (rc < 0) {
 			APR(("Can't register Transparent Print device (%d)\n", rc));
-			return(rc);
+			goto err_put_tty_print;
+			goto err_unregister_serial;
 		}
 		brd->dgap_Major_TransparentPrint_Registered = TRUE;
 		dgap_BoardsByMajor[brd->PrintDriver->major] = brd;
@@ -306,7 +316,18 @@ int dgap_tty_register(struct board_t *brd)
 
 	DPR_INIT(("DGAP REGISTER TTY: MAJORS: %d %d\n", brd->SerialDriver->major,
 		brd->PrintDriver->major));
-
+	return 0;
+
+err_unregister_serial:
+        tty_unregister_driver(brd->SerialDriver);
+err_free_print_ttys:
+        kfree(brd->PrintDriver->ttys);
+err_put_tty_print:
+        put_tty_driver(brd->PrintDriver);
+err_free_serial_ttys:
+        kfree(brd->SerialDriver->ttys);
+err_put_tty_serial:
+        put_tty_driver(brd->SerialDriver);
 	return (rc);
 }
 
-- 
1.8.1.2

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel




[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux