char: scx200_gpio: check cdev_add() return value

cdev_add() can fail. The driver currently ignores the return value and
reports probe success even when the cdev was not registered, leaving a
chrdev region allocated with no working file operations.

Check the return value, emit an error, and unwind the chrdev region and
platform device on failure.

Fixes: 635adb6cd2 ("[PATCH] scx200_gpio: use 1 cdev for N minors, not N for N")
Signed-off-by: Linkai Gong <gonglinkai@kylinos.cn>
Link: https://patch.msgid.link/20260803015450.2050900-1-gonglinkai@kylinos.cn
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Linkai Gong 2026-08-03 09:54:50 +08:00 committed by Greg Kroah-Hartman
parent eddbbe3ecb
commit fe26c195c2

View File

@ -107,10 +107,16 @@ 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) {
dev_err(&pdev->dev, "cdev_add err: %d\n", rc);
goto undo_chrdev_region;
}
return 0; /* succeed */
undo_chrdev_region:
unregister_chrdev_region(devid, MAX_PINS);
undo_platform_device_add:
platform_device_del(pdev);
undo_malloc: