[PATCH 5/6] staging: dgap: tty.c: removes smatch warnings "potential null dereference"

Lidza Louina lidza.louina at gmail.com
Mon Sep 23 22:47:16 UTC 2013


This patch removes these warnings:
potential null dereference 'brd->SerialDriver'. (alloc_tty_driver returns null)
potential null dereference 'brd->PrintDriver'. (alloc_tty_driver returns null)

This warning popped up because there wasn't a check
to make sure that the serial and print drivers were
allocated and not null before being initialized. This
patch adds that check.

Signed-off-by: Lidza Louina <lidza.louina at gmail.com>
---
 drivers/staging/dgap/dgap_tty.c | 103 +++++++++++++++++++++-------------------
 1 file changed, 54 insertions(+), 49 deletions(-)

diff --git a/drivers/staging/dgap/dgap_tty.c b/drivers/staging/dgap/dgap_tty.c
index 924e2bf..8f0a824 100644
--- a/drivers/staging/dgap/dgap_tty.c
+++ b/drivers/staging/dgap/dgap_tty.c
@@ -220,66 +220,71 @@ int dgap_tty_register(struct board_t *brd)
 	DPR_INIT(("tty_register start"));
 
 	brd->SerialDriver = alloc_tty_driver(MAXPORTS);
+	if(brd->SerialDriver){
+		snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgap_%d_", brd->boardnum);
+		brd->SerialDriver->name = brd->SerialName;
+		brd->SerialDriver->name_base = 0;
+		brd->SerialDriver->major = 0;
+		brd->SerialDriver->minor_start = 0;
+		brd->SerialDriver->type = TTY_DRIVER_TYPE_SERIAL; 
+		brd->SerialDriver->subtype = SERIAL_TYPE_NORMAL;   
+		brd->SerialDriver->init_termios = DgapDefaultTermios;
+		brd->SerialDriver->driver_name = DRVSTR;
+		brd->SerialDriver->flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
+
+		/* 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 LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
+		brd->SerialDriver->refcount = brd->TtyRefCnt;
+	#endif
 
-	snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgap_%d_", brd->boardnum);
-	brd->SerialDriver->name = brd->SerialName;
-	brd->SerialDriver->name_base = 0;
-	brd->SerialDriver->major = 0;
-	brd->SerialDriver->minor_start = 0;
-	brd->SerialDriver->type = TTY_DRIVER_TYPE_SERIAL; 
-	brd->SerialDriver->subtype = SERIAL_TYPE_NORMAL;   
-	brd->SerialDriver->init_termios = DgapDefaultTermios;
-	brd->SerialDriver->driver_name = DRVSTR;
-	brd->SerialDriver->flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
-
-	/* 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)
+		/*
+		 * Entry points for driver.  Called by the kernel from
+		 * tty_io.c and n_tty.c.
+		 */
+		tty_set_operations(brd->SerialDriver, &dgap_tty_ops);
+	}
+	else
 		return(-ENOMEM);
 
-#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
-	brd->SerialDriver->refcount = brd->TtyRefCnt;
-#endif
-
-	/*
-	 * Entry points for driver.  Called by the kernel from
-	 * tty_io.c and n_tty.c.
-	 */
-	tty_set_operations(brd->SerialDriver, &dgap_tty_ops);
-
 	/*
 	 * If we're doing transparent print, we have to do all of the above
 	 * again, separately so we don't get the LD confused about what major
 	 * we are when we get into the dgap_tty_open() routine.
 	 */
 	brd->PrintDriver = alloc_tty_driver(MAXPORTS);
+	if(brd->PrintDriver){
+		snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgap_%d_", brd->boardnum);
+		brd->PrintDriver->name = brd->PrintName;
+		brd->PrintDriver->name_base = 0;
+		brd->PrintDriver->major = 0;
+		brd->PrintDriver->minor_start = 0;
+		brd->PrintDriver->type = TTY_DRIVER_TYPE_SERIAL;   
+		brd->PrintDriver->subtype = SERIAL_TYPE_NORMAL;
+		brd->PrintDriver->init_termios = DgapDefaultTermios;
+		brd->PrintDriver->driver_name = DRVSTR;
+		brd->PrintDriver->flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
+
+		/* 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 LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
+		brd->PrintDriver->refcount = brd->TtyRefCnt;
+	#endif
 
-	snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgap_%d_", brd->boardnum);
-	brd->PrintDriver->name = brd->PrintName;
-	brd->PrintDriver->name_base = 0;
-	brd->PrintDriver->major = 0;
-	brd->PrintDriver->minor_start = 0;
-	brd->PrintDriver->type = TTY_DRIVER_TYPE_SERIAL;   
-	brd->PrintDriver->subtype = SERIAL_TYPE_NORMAL;
-	brd->PrintDriver->init_termios = DgapDefaultTermios;
-	brd->PrintDriver->driver_name = DRVSTR;
-	brd->PrintDriver->flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
-
-	/* 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)
+		/*
+		 * Entry points for driver.  Called by the kernel from
+		 * tty_io.c and n_tty.c.
+		 */
+		tty_set_operations(brd->PrintDriver, &dgap_tty_ops);
+	}
+	else
 		return(-ENOMEM);
-
-#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
-	brd->PrintDriver->refcount = brd->TtyRefCnt;
-#endif
-
-	/*
-	 * Entry points for driver.  Called by the kernel from
-	 * tty_io.c and n_tty.c.
-	 */
-	tty_set_operations(brd->PrintDriver, &dgap_tty_ops);
-
 	if (!brd->dgap_Major_Serial_Registered) {
 		/* Register tty devices */
 		rc = tty_register_driver(brd->SerialDriver);
-- 
1.8.1.2



More information about the devel mailing list