[PATCH] char: dsp56k: check device_create() return value

Linkai Gong posted 1 patch 1 week, 4 days ago
drivers/char/dsp56k.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
[PATCH] char: dsp56k: check device_create() return value
Posted by Linkai Gong 1 week, 4 days ago
device_create() can fail, but the driver ignored the return value and
still reported init success. That leaves a registered char device
without its /dev node.

Check the return value, report the error, and unwind the class and
chrdev registration on failure.

Signed-off-by: Linkai Gong <gonglinkai@kylinos.cn>
---
 drivers/char/dsp56k.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/char/dsp56k.c b/drivers/char/dsp56k.c
index 1c2c8439797c..cbcfef44a075 100644
--- a/drivers/char/dsp56k.c
+++ b/drivers/char/dsp56k.c
@@ -509,12 +509,20 @@ static int __init dsp56k_init_driver(void)
 	err = class_register(&dsp56k_class);
 	if (err)
 		goto out_chrdev;
-	device_create(&dsp56k_class, NULL, MKDEV(DSP56K_MAJOR, 0), NULL,
-		      "dsp56k");
+
+	err = PTR_ERR_OR_ZERO(device_create(&dsp56k_class, NULL,
+					    MKDEV(DSP56K_MAJOR, 0), NULL,
+					    "dsp56k"));
+	if (err) {
+		pr_err("DSP56k driver: Unable to create device\n");
+		goto out_class;
+	}
 
 	printk(banner);
 	goto out;
 
+out_class:
+	class_unregister(&dsp56k_class);
 out_chrdev:
 	unregister_chrdev(DSP56K_MAJOR, "dsp56k");
 out:
-- 
2.25.1