Skip to contentSkip to conversation

autofs: replace manual symlink buffer allocation in autofs_dir_symlink

PATCH RESEND Superseded Thorsten Blum 2 participants 3 messages Jan 27 – Feb 22 Kernel automounter

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 autofs: replace manual symlink buffer allocation in autofs_dir_symlink

The symlink name was previously duplicated using an explicit kmalloc() followed by strcpy(), which is deprecated [1]. Replace this open-coded string duplication with kstrdup(), which allocates and copies the symlink name with a single helper function.

Remove the local variable 'size' and set 'i_size' directly using strlen(cp), which is equivalent to the previous value of 'size'.

This simplifies the code, uses common string-handling helpers, and removes the deprecated use of strcpy().

---
 fs/autofs/root.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
fs/autofs/root.c +3 -5
index 2c31002b314a..186e960f1e23 100644
@@ -7,6 +7,7 @@
 
 #include <linux/capability.h>
 #include <linux/compat.h>
+#include <linux/string.h>
 
 #include "autofs_i.h"
 
@@ -578,7 +579,6 @@ static int autofs_dir_symlink(struct mnt_idmap *idmap,
 	struct autofs_info *ino = autofs_dentry_ino(dentry);
 	struct autofs_info *p_ino;
 	struct inode *inode;
-	size_t size = strlen(symname);
 	char *cp;
 
 	pr_debug("%s <- %pd\n", symname, dentry);
@@ -589,19 +589,17 @@ static int autofs_dir_symlink(struct mnt_idmap *idmap,
 
 	autofs_del_active(dentry);
 
-	cp = kmalloc(size + 1, GFP_KERNEL);
+	cp = kstrdup(symname, GFP_KERNEL);
 	if (!cp)
 		return -ENOMEM;
 
-	strcpy(cp, symname);
-
 	inode = autofs_get_inode(dir->i_sb, S_IFLNK | 0555);
 	if (!inode) {
 		kfree(cp);
 		return -ENOMEM;
 	}
 	inode->i_private = cp;
-	inode->i_size = size;
+	inode->i_size = strlen(cp);
 
 	d_make_persistent(dentry, inode);
 	p_ino = autofs_dentry_ino(dentry->d_parent);
Signature · 2 lines
Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4
maintainer
#1 lore raw
Quoted 49 lines · Thorsten Blum

The symlink name was previously duplicated using an explicit kmalloc() followed by strcpy(), which is deprecated [1]. Replace this open-coded string duplication with kstrdup(), which allocates and copies the symlink name with a single helper function.

Remove the local variable 'size' and set 'i_size' directly using strlen(cp), which is equivalent to the previous value of 'size'.

This simplifies the code, uses common string-handling helpers, and removes the deprecated use of strcpy().

---
  fs/autofs/root.c | 8 +++-----
  1 file changed, 3 insertions(+), 5 deletions(-)
fs/autofs/root.c +2 -4
index 2c31002b314a..186e960f1e23 100644
@@ -7,6 +7,7 @@
 
  #include <linux/capability.h>
  #include <linux/compat.h>
+#include <linux/string.h>
 
  #include "autofs_i.h"
 
@@ -578,7 +579,6 @@ static int autofs_dir_symlink(struct mnt_idmap *idmap,
  	struct autofs_info *ino = autofs_dentry_ino(dentry);
  	struct autofs_info *p_ino;
  	struct inode *inode;
-	size_t size = strlen(symname);
  	char *cp;
 
  	pr_debug("%s <- %pd\n", symname, dentry);
@@ -589,19 +589,17 @@ static int autofs_dir_symlink(struct mnt_idmap *idmap,
 
  	autofs_del_active(dentry);
 
-	cp = kmalloc(size + 1, GFP_KERNEL);
+	cp = kstrdup(symname, GFP_KERNEL);
  	if (!cp)
  		return -ENOMEM;
 
-	strcpy(cp, symname);
-
  	inode = autofs_get_inode(dir->i_sb, S_IFLNK | 0555);

fs/autofs/root.c static int autofs_dir_symlink(struct mnt_idmap *idmap, · line 605

Quoted 10 lines
fs/autofs/root.c +1 -1
  	if (!inode) {
  		kfree(cp);
  		return -ENOMEM;
  	}
  	inode->i_private = cp;
-	inode->i_size = size;
+	inode->i_size = strlen(cp);
 
  	d_make_persistent(dentry, inode);
  	p_ino = autofs_dentry_ino(dentry->d_parent);

Looks fine to me.

Acked by: Ian Kent <[email protected]>

Christian, is there anything else I need to do for this?

Ian

author Acked-by Ian Kent
#2 lore raw
Quoted 18 lines · Ian Kent
Quoted 17 lines · Thorsten Blum

The symlink name was previously duplicated using an explicit kmalloc() followed by strcpy(), which is deprecated [1]. Replace this open-coded string duplication with kstrdup(), which allocates and copies the symlink name with a single helper function.

Remove the local variable 'size' and set 'i_size' directly using strlen(cp), which is equivalent to the previous value of 'size'.

This simplifies the code, uses common string-handling helpers, and removes the deprecated use of strcpy().

---
 fs/autofs/root.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
Quoted 5 lines
Quoted 1 line

[...]

Looks fine to me.

Acked by: Ian Kent <[email protected]>

Your tag is missing a hyphen and tools like b4 don't recognize it like this. Correct would be:

Quoted 1 line

Christian, is there anything else I need to do for this?

Thanks,
Thorsten