[PATCH v2] Fix error handling in dgnc_start()

Devendra Naga devendranaga4 at gmail.com
Sat Oct 18 21:07:28 UTC 2014


The error_handling for class_create, device_create are taken care
by the respective goto statements. The failure code from class_create and
device_create are assigned to the variable rc to return a proper value.

Also removed the rc initialisation to 0 as it gets changed in the call to
register_chrdev.

Signed-off-by: Devendra Naga <devendranaga4 at gmail.com>
---

 Changes since v2: (added solutions for Dan's comments)

 1. removed error message print
 2. added error handling in device_create
 3. added goto err_device_destroy when dgnc_tty_preinit fails


 drivers/staging/dgnc/dgnc_driver.c |   28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/drivers/staging/dgnc/dgnc_driver.c b/drivers/staging/dgnc/dgnc_driver.c
index 2154665..e0fd0ea 100644
--- a/drivers/staging/dgnc/dgnc_driver.c
+++ b/drivers/staging/dgnc/dgnc_driver.c
@@ -248,8 +248,9 @@ module_exit(dgnc_cleanup_module);
  */
 static int dgnc_start(void)
 {
-	int rc = 0;
+	int rc;
 	unsigned long flags;
+	struct device *dev;
 
 	/* make sure that the globals are init'd before we do anything else */
 	dgnc_init_globals();
@@ -271,18 +272,25 @@ static int dgnc_start(void)
 	dgnc_Major = rc;
 
 	dgnc_class = class_create(THIS_MODULE, "dgnc_mgmt");
-	device_create(dgnc_class, NULL,
+	if (IS_ERR(dgnc_class)) {
+		rc = PTR_ERR(dgnc_class);
+		goto err_unregister;
+	}
+
+	dev = device_create(dgnc_class, NULL,
 		MKDEV(dgnc_Major, 0),
 		NULL, "dgnc_mgmt");
-
+	if (IS_ERR(dev)) {
+		rc = PTR_ERR(dev);
+		goto err_class_destroy;
+	}
 	/*
 	 * Init any global tty stuff.
 	 */
 	rc = dgnc_tty_preinit();
-
 	if (rc < 0) {
 		APR(("tty preinit - not enough memory (%d)\n", rc));
-		return rc;
+		goto err_device_destroy;
 	}
 
 	/* Start the poller */
@@ -296,6 +304,16 @@ static int dgnc_start(void)
 
 	add_timer(&dgnc_poll_timer);
 
+	return 0;
+
+err_device_destroy:
+	device_destroy(dgnc_class, MKDEV(dgnc_Major, 0));
+
+err_class_destroy:
+	class_destroy(dgnc_class);
+
+err_unregister:
+	unregister_chrdev(dgnc_Major, "dgnc");
 	return rc;
 }
 
-- 
1.7.9.5



More information about the devel mailing list