Skip to content

Commit e19994d

Browse files
authored
Merge pull request #362 from EricccTaiwan/err-check
Add error handling for class_create()
2 parents f8c730d + fdf50cc commit e19994d

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

examples/chardev.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ static int __init chardev_init(void)
6767
#else
6868
cls = class_create(THIS_MODULE, DEVICE_NAME);
6969
#endif
70+
if (IS_ERR(cls)) {
71+
pr_err("Failed to create class for device\n");
72+
unregister_chrdev(major, DEVICE_NAME);
73+
return PTR_ERR(cls);
74+
}
7075
device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
7176

7277
pr_info("Device created on /dev/%s\n", DEVICE_NAME);

examples/chardev2.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ static int __init chardev2_init(void)
208208
#else
209209
cls = class_create(THIS_MODULE, DEVICE_FILE_NAME);
210210
#endif
211+
if (IS_ERR(cls)) {
212+
pr_err("Failed to create class for device\n");
213+
unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
214+
return PTR_ERR(cls);
215+
}
211216
device_create(cls, NULL, MKDEV(MAJOR_NUM, 0), NULL, DEVICE_FILE_NAME);
212217

213218
pr_info("Device created on /dev/%s\n", DEVICE_FILE_NAME);

examples/static_key.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ static int __init chardev_init(void)
6363
#else
6464
cls = class_create(THIS_MODULE, DEVICE_NAME);
6565
#endif
66-
66+
if (IS_ERR(cls)) {
67+
pr_err("Failed to create class for device\n");
68+
unregister_chrdev(major, DEVICE_NAME);
69+
return PTR_ERR(cls);
70+
}
6771
device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
6872

6973
pr_info("Device created on /dev/%s\n", DEVICE_NAME);

0 commit comments

Comments
 (0)