[RFC PATCH] char: misc: Init misc->list in a safe way

From: Orson Zhai
Date: Mon Jun 26 2017 - 05:35:15 EST


From: Zhongping Tan <zhongping.tan@xxxxxxxxxxxxxx>

It is likely to enter a wrong case and return an error when registerring
a misc device. As a result, misc->list will be intialized to a dead loop
which is possible to go into wrong situation if anyone refers to it else
where.

Move the initializion line out of all error branches to avoid any side
effect.

Signed-off-by: Zhongping Tan <zhongping.tan@xxxxxxxxxxxxxx>
Signed-off-by: Orson Zhai <orson.zhai@xxxxxxxxxxxxxx>
---
drivers/char/misc.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index c9cd1ea6844a..876e7d57cc6c 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -188,8 +188,6 @@ int misc_register(struct miscdevice *misc)
int err = 0;
bool is_dynamic = (misc->minor == MISC_DYNAMIC_MINOR);

- INIT_LIST_HEAD(&misc->list);
-
mutex_lock(&misc_mtx);

if (is_dynamic) {
@@ -233,6 +231,7 @@ int misc_register(struct miscdevice *misc)
* Add it to the front, so that later devices can "override"
* earlier defaults
*/
+ INIT_LIST_HEAD(&misc->list);
list_add(&misc->list, &misc_list);
out:
mutex_unlock(&misc_mtx);
--
2.12.2