Skip to contentSkip to conversation

drivers: char: scx200_gpio: check return value of cdev_add()

PATCH In review Salah Triki 2 participants 4 messages Apr 7, 2025 – Apr 15, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers: char: scx200_gpio: check return value of cdev_add()

Check return value of cdev_add() and in case of error unregister the range of device numbers.

---
 drivers/char/scx200_gpio.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/char/scx200_gpio.c +5 -1
index 700e6affea6f..36efcc828e8e 100644
@@ -107,10 +107,14 @@ static int __init scx200_gpio_init(void)
 	}
 
 	cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
-	cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
+	rc = cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
+	if (rc)
+		goto unregister_chrdev_region;
 
 	return 0; /* succeed */
 
+unregister_chrdev_region:
+	unregister_chrdev_region(devid, MAX_PINS);
 undo_platform_device_add:
 	platform_device_del(pdev);
 undo_malloc:
Signature · 1 line
2.43.0
#1 lore raw
Quoted 31 lines · Salah Triki

Check return value of cdev_add() and in case of error unregister the range of device numbers.

---
 drivers/char/scx200_gpio.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/char/scx200_gpio.c +5 -1
index 700e6affea6f..36efcc828e8e 100644
@@ -107,10 +107,14 @@ static int __init scx200_gpio_init(void)
 	}
 
 	cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
-	cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
+	rc = cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
+	if (rc)
+		goto unregister_chrdev_region;
 
 	return 0; /* succeed */
 
+unregister_chrdev_region:
+	unregister_chrdev_region(devid, MAX_PINS);
 undo_platform_device_add:
 	platform_device_del(pdev);
 undo_malloc:
Signature · 1 line
2.43.0

How was this tested?

thanks,

greg k-h

#2 lore raw
Quoted 24 lines · Greg Kroah-Hartman
Quoted 23 lines · Salah Triki

Check return value of cdev_add() and in case of error unregister the range of device numbers.

---
 drivers/char/scx200_gpio.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/char/scx200_gpio.c +3 -1
index 700e6affea6f..36efcc828e8e 100644
@@ -107,10 +107,14 @@ static int __init scx200_gpio_init(void)
 	}
 
 	cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
-	cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
+	rc = cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
+	if (rc)
+		goto unregister_chrdev_region;
 
 	return 0; /* succeed */
 
Quoted 11 lines
Quoted 8 lines
drivers/char/scx200_gpio.c +2 -0
+unregister_chrdev_region:
+	unregister_chrdev_region(devid, MAX_PINS);
 undo_platform_device_add:
 	platform_device_del(pdev);
 undo_malloc:
Signature · 1 line
2.43.0

How was this tested?

I only compiled it.

ST

Quoted 3 lines

thanks,

greg k-h

#3 lore raw
Quoted 37 lines · Salah Triki
Quoted 24 lines · Greg Kroah-Hartman
Quoted 23 lines · Salah Triki

Check return value of cdev_add() and in case of error unregister the range of device numbers.

---
 drivers/char/scx200_gpio.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/char/scx200_gpio.c +3 -1
index 700e6affea6f..36efcc828e8e 100644
@@ -107,10 +107,14 @@ static int __init scx200_gpio_init(void)
 	}
 
 	cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
-	cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
+	rc = cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
+	if (rc)
+		goto unregister_chrdev_region;
 
 	return 0; /* succeed */
 
Quoted 11 lines
Quoted 8 lines
drivers/char/scx200_gpio.c +2 -0
+unregister_chrdev_region:
+	unregister_chrdev_region(devid, MAX_PINS);
 undo_platform_device_add:
 	platform_device_del(pdev);
 undo_malloc:
Signature · 1 line
2.43.0

How was this tested?

Quoted 1 line

I only compiled it.

Please properly test it.