Skip to contentSkip to conversation

staging: dgap: implement error handling in dgap_tty_register()

PATCH Superseded Daeseok Youn 2 participants 5 messages May 19, 2014 Staging

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 staging: dgap: implement error handling in dgap_tty_register()

- alloc_tty_driver() is deprecated so it is changed to tty_alloc_driver()
- Pointers which are allocated by alloc_tty_driver() and kzalloc() can be NULL so it need to check NULL for them.
- If one of those is failed, it need to add proper handler for avoiding memory leak.
- If both of drivers are registered normally, and
then set TRUE to dgap_major_serial{print}_registered. If one of drivers is failed to register, leave a default value as FALSE.

---
V2: rebased on staging-next branch
V3: removes unneeded information in e-mail

 drivers/staging/dgap/dgap.c |   49 +++++++++++++++++++++++++++++++++---------
 1 files changed, 38 insertions(+), 11 deletions(-)
drivers/staging/dgap/dgap.c +38 -11
index d7cfc45..e4cb7aa 100644
@@ -1212,7 +1212,9 @@ static int dgap_tty_register(struct board_t *brd)
 {
 	int rc = 0;
 
-	brd->serial_driver = alloc_tty_driver(MAXPORTS);
+	brd->serial_driver = tty_alloc_driver(MAXPORTS, 0);
+	if (IS_ERR(brd->serial_driver))
+		return PTR_ERR(brd->serial_driver);
 
 	snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgap_%d_",
 		 brd->boardnum);
@@ -1231,8 +1233,10 @@ static int dgap_tty_register(struct board_t *brd)
 	/* The kernel wants space to store pointers to tty_structs */
 	brd->serial_driver->ttys =
 		kzalloc(MAXPORTS * sizeof(struct tty_struct *), GFP_KERNEL);
-	if (!brd->serial_driver->ttys)
-		return -ENOMEM;
+	if (!brd->serial_driver->ttys) {
+		rc = -ENOMEM;
+		goto free_serial_drv;
+	}
 
 	/*
 	 * Entry points for driver.  Called by the kernel from
@@ -1245,7 +1249,11 @@ static int dgap_tty_register(struct board_t *brd)
 	 * 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->print_driver = alloc_tty_driver(MAXPORTS);
+	brd->print_driver = tty_alloc_driver(MAXPORTS, 0);
+	if (IS_ERR(brd->print_driver)) {
+		rc = PTR_ERR(brd->print_driver);
+		goto free_serial_ttys;
+	}
 
 	snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgap_%d_",
 		 brd->boardnum);
@@ -1264,8 +1272,10 @@ static int dgap_tty_register(struct board_t *brd)
 	/* The kernel wants space to store pointers to tty_structs */
 	brd->print_driver->ttys =
 		kzalloc(MAXPORTS * sizeof(struct tty_struct *), GFP_KERNEL);
-	if (!brd->print_driver->ttys)
-		return -ENOMEM;
+	if (!brd->print_driver->ttys) {
+		rc = -ENOMEM;
+		goto free_print_drv;
+	}
 
 	/*
 	 * Entry points for driver.  Called by the kernel from
@@ -1276,20 +1286,37 @@ static int dgap_tty_register(struct board_t *brd)
 	/* Register tty devices */
 	rc = tty_register_driver(brd->serial_driver);
 	if (rc < 0)
-		return rc;
-	brd->dgap_major_serial_registered = TRUE;
-	dgap_boards_by_major[brd->serial_driver->major] = brd;
-	brd->dgap_serial_major = brd->serial_driver->major;
+		goto free_print_ttys;
 
 	/* Register Transparent Print devices */
 	rc = tty_register_driver(brd->print_driver);
 	if (rc < 0)
-		return rc;
+		goto unregister_serial_drv;
+
+	brd->dgap_major_serial_registered = TRUE;
+	dgap_boards_by_major[brd->serial_driver->major] = brd;
+	brd->dgap_serial_major = brd->serial_driver->major;
+
 	brd->dgap_major_transparent_print_registered = TRUE;
 	dgap_boards_by_major[brd->print_driver->major] = brd;
 	brd->dgap_transparent_print_major = brd->print_driver->major;
 
 	return rc;
+
+unregister_serial_drv:
+	tty_unregister_driver(brd->serial_driver);
+free_print_ttys:
+	kfree(brd->print_driver->ttys);
+	brd->print_driver->ttys = NULL;
+free_print_drv:
+	put_tty_driver(brd->print_driver);
+free_serial_ttys:
+	kfree(brd->serial_driver->ttys);
+	brd->serial_driver->ttys = NULL;
+free_serial_drv:
+	put_tty_driver(brd->serial_driver);
+
+	return rc;
 }
 
 /*
Signature · 1 line
1.7.1
#1 lore raw

Nice, but it needs a couple style improvements below.

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1304

Quoted 9 lines · Daeseok Youn
drivers/staging/dgap/dgap.c +4 -0
+	brd->dgap_major_serial_registered = TRUE;
+	dgap_boards_by_major[brd->serial_driver->major] = brd;
+	brd->dgap_serial_major = brd->serial_driver->major;
+
 	brd->dgap_major_transparent_print_registered = TRUE;
 	dgap_boards_by_major[brd->print_driver->major] = brd;
 	brd->dgap_transparent_print_major = brd->print_driver->major;
 
 	return rc;
	return 0;

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1310

Quoted 6 lines
drivers/staging/dgap/dgap.c +6 -0
+
+unregister_serial_drv:
+	tty_unregister_driver(brd->serial_driver);
+free_print_ttys:
+	kfree(brd->print_driver->ttys);
+	brd->print_driver->ttys = NULL;

This label isn't needed. Just goto free_print_drv, because that will free the brd->print_driver->ttys in destruct_tty_driver(). I do like how you noticed the double free and avoided it by setting brd->serial_driver->ttys to NULL, so your patch doesn't introduce a double free bug.

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1315

Quoted 5 lines
drivers/staging/dgap/dgap.c +5 -0
+free_print_drv:
+	put_tty_driver(brd->print_driver);
+free_serial_ttys:
+	kfree(brd->serial_driver->ttys);
+	brd->serial_driver->ttys = NULL;

Same for this.

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1319

Quoted 4 lines
drivers/staging/dgap/dgap.c +4 -0
+free_serial_drv:
+	put_tty_driver(brd->serial_driver);
+
+	return rc;

regards,
dan carpenter

#2 lore raw

2014-05-19 17:02 GMT+09:00 Dan Carpenter <[email protected]>:

Quoted 11 lines

Nice, but it needs a couple style improvements below.

Quoted 8 lines · Daeseok Youn
drivers/staging/dgap/dgap.c +4 -0
+     brd->dgap_major_serial_registered = TRUE;
+     dgap_boards_by_major[brd->serial_driver->major] = brd;
+     brd->dgap_serial_major = brd->serial_driver->major;
+
      brd->dgap_major_transparent_print_registered = TRUE;
      dgap_boards_by_major[brd->print_driver->major] = brd;
      brd->dgap_transparent_print_major = brd->print_driver->major;
Quoted 3 lines
Quoted 1 line
      return rc;
        return 0;

OK. I will remove "int rc = 0" line and change "return rc" to "return 0"

Quoted 13 lines

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1310

Quoted 7 lines
drivers/staging/dgap/dgap.c +6 -0
+
+unregister_serial_drv:
+     tty_unregister_driver(brd->serial_driver);
+free_print_ttys:
+     kfree(brd->print_driver->ttys);
+     brd->print_driver->ttys = NULL;

This label isn't needed. Just goto free_print_drv, because that will free the brd->print_driver->ttys in destruct_tty_driver(). I do like how you noticed the double free and avoided it by setting brd->serial_driver->ttys to NULL, so your patch doesn't introduce a double free bug.

Yes, just goto free_print_drv.

Thanks for review.

Regards,
Daeseok Youn

Quoted 16 lines

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1315

Quoted 6 lines
drivers/staging/dgap/dgap.c +5 -0
+free_print_drv:
+     put_tty_driver(brd->print_driver);
+free_serial_ttys:
+     kfree(brd->serial_driver->ttys);
+     brd->serial_driver->ttys = NULL;

Same for this.

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1319

Quoted 4 lines
drivers/staging/dgap/dgap.c +4 -0
+free_serial_drv:
+     put_tty_driver(brd->serial_driver);
+
+     return rc;

regards,
dan carpenter

#3 lore raw

2014-05-19 18:39 GMT+09:00 DaeSeok Youn <[email protected]>:

Quoted 12 lines

2014-05-19 17:02 GMT+09:00 Dan Carpenter <[email protected]>:

Quoted 11 lines

Nice, but it needs a couple style improvements below.

Quoted 8 lines · Daeseok Youn
drivers/staging/dgap/dgap.c +4 -0
+     brd->dgap_major_serial_registered = TRUE;
+     dgap_boards_by_major[brd->serial_driver->major] = brd;
+     brd->dgap_serial_major = brd->serial_driver->major;
+
      brd->dgap_major_transparent_print_registered = TRUE;
      dgap_boards_by_major[brd->print_driver->major] = brd;
      brd->dgap_transparent_print_major = brd->print_driver->major;
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
      return rc;
        return 0;

OK. I will remove "int rc = 0" line and change "return rc" to "return 0"

Oh.. just remove the initialization to zero.

Quoted 36 lines
Quoted 14 lines

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1310

Quoted 8 lines
drivers/staging/dgap/dgap.c +6 -0
+
+unregister_serial_drv:
+     tty_unregister_driver(brd->serial_driver);
+free_print_ttys:
+     kfree(brd->print_driver->ttys);
+     brd->print_driver->ttys = NULL;

This label isn't needed. Just goto free_print_drv, because that will free the brd->print_driver->ttys in destruct_tty_driver(). I do like how you noticed the double free and avoided it by setting brd->serial_driver->ttys to NULL, so your patch doesn't introduce a double free bug.

Yes, just goto free_print_drv.

Thanks for review.

Regards,
Daeseok Youn

Quoted 16 lines

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1315

Quoted 6 lines
drivers/staging/dgap/dgap.c +5 -0
+free_print_drv:
+     put_tty_driver(brd->print_driver);
+free_serial_ttys:
+     kfree(brd->serial_driver->ttys);
+     brd->serial_driver->ttys = NULL;

Same for this.

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1319

Quoted 4 lines
drivers/staging/dgap/dgap.c +4 -0
+free_serial_drv:
+     put_tty_driver(brd->serial_driver);
+
+     return rc;

regards,
dan carpenter

#4 lore raw
In reply to Daeseok Youn

drivers/staging/dgap/dgap.c static int dgap_tty_register(struct board_t *brd) · line 1213

Quoted 7 lines · Daeseok Youn
drivers/staging/dgap/dgap.c +0 -0
index d7cfc45..e4cb7aa 100644
@@ -1212,7 +1212,9 @@ static int dgap_tty_register(struct board_t *brd)
 {
 	int rc = 0;

Oh, and you may as well remove this initialization while you are at it.

regards,
dan carpenter