[PATCH v2] chardev: check return value of kobject_set_name() in __register_chrdev()

phucduc.bui@gmail.com posted 1 patch 4 weeks, 1 day ago
fs/char_dev.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH v2] chardev: check return value of kobject_set_name() in __register_chrdev()
Posted by phucduc.bui@gmail.com 4 weeks, 1 day ago
From: bui duc phuc <phucduc.bui@gmail.com>

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.

Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: bui duc phuc <phucduc.bui@gmail.com>
---

Changes in v2:
 - Add Reviewed-by tag from Jan Kara.

 fs/char_dev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/char_dev.c b/fs/char_dev.c
index 00229e25c10f..5ce5423f6c99 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -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)
-- 
2.43.0
Re: [PATCH v2] chardev: check return value of kobject_set_name() in __register_chrdev()
Posted by Christian Brauner 3 weeks, 1 day ago
On Fri, 28 Aug 2026 16:27:34 +0700, phucduc.bui@gmail.com wrote:
> chardev: check return value of kobject_set_name() in __register_chrdev()

Applied to the vfs-7.4.misc branch of the vfs/vfs.git tree.
Patches in the vfs-7.4.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.4.misc

[1/1] chardev: check return value of kobject_set_name() in __register_chrdev()
      https://git.kernel.org/vfs/vfs/c/e0bec04067a9