Skip to contentSkip to conversation

chardev: check return value of kobject_set_name() in __register_chrdev()

PATCH Superseded [email protected] 2 participants 3 messages Aug 26 – Aug 27 VFS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 chardev: check return value of kobject_set_name() in __register_chrdev()

From: bui duc phuc <[email protected]>

The kobject_set_name() function can fail and return -ENOMEM if memory allocation fails.

While all other functions called inside __register_chrdev() are properly checked for errors, kobject_set_name() was missing this check. Add the error check for kobject_set_name() and jump to the 'out' label on failure to maintain consistency and ensure proper error handling.

Found by manual code inspection.

---
 fs/char_dev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/char_dev.c +3 -1
index 00229e25c10f..5ce5423f6c99 100644
@@ -280,7 +280,9 @@ int __register_chrdev(unsigned int major, unsigned int baseminor,
 
 	cdev->owner = fops->owner;
 	cdev->ops = fops;
-	kobject_set_name(&cdev->kobj, "%s", name);
+	err = kobject_set_name(&cdev->kobj, "%s", name);
+	if (err)
+		goto out;
 
 	err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
 	if (err)
Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · On Wed 26-08-26 12:20:55, [email protected] wrote:

From: bui duc phuc <[email protected]>

The kobject_set_name() function can fail and return -ENOMEM if memory allocation fails.

Quoted 8 lines

While all other functions called inside __register_chrdev() are properly checked for errors, kobject_set_name() was missing this check. Add the error check for kobject_set_name() and jump to the 'out' label on failure to maintain consistency and ensure proper error handling.

Found by manual code inspection.

Looks good. Feel free to add:

Honza

Quoted 22 lines
---
 fs/char_dev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/char_dev.c +3 -1
index 00229e25c10f..5ce5423f6c99 100644
@@ -280,7 +280,9 @@ int __register_chrdev(unsigned int major, unsigned int baseminor,
 
 	cdev->owner = fops->owner;
 	cdev->ops = fops;
-	kobject_set_name(&cdev->kobj, "%s", name);
+	err = kobject_set_name(&cdev->kobj, "%s", name);
+	if (err)
+		goto out;
 
 	err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
 	if (err)
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR