Skip to contentSkip to conversation

ecryptfs: Replace memcpy + NUL termination in ecryptfs_copy_filename

PATCH In review Thorsten Blum 2 participants 2 messages Jan 11 – Jan 13 Ecrypt file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ecryptfs: Replace memcpy + NUL termination in ecryptfs_copy_filename

Use kmemdup_nul() to copy 'name' instead of using memcpy() followed by a manual NUL termination. Remove the local return variable and the goto label to simplify the code. No functional changes.

---
 fs/ecryptfs/crypto.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
fs/ecryptfs/crypto.c +4 -14
index d49cdf7292ab..82fc5e1b6324 100644
@@ -1422,21 +1422,11 @@ ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
 static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
 				  const char *name, size_t name_size)
 {
-	int rc = 0;
-
-	(*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
-	if (!(*copied_name)) {
-		rc = -ENOMEM;
-		goto out;
-	}
-	memcpy((void *)(*copied_name), (void *)name, name_size);
-	(*copied_name)[(name_size)] = '\0';	/* Only for convenience
-						 * in printing out the
-						 * string in debug
-						 * messages */
+	(*copied_name) = kmemdup_nul(name, name_size, GFP_KERNEL);
+	if (!(*copied_name))
+		return -ENOMEM;
 	(*copied_name_size) = name_size;
-out:
-	return rc;
+	return 0;
 }
 
 /**
Signature · 2 lines
Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Thorsten Blum

Use kmemdup_nul() to copy 'name' instead of using memcpy() followed by a manual NUL termination. Remove the local return variable and the goto label to simplify the code. No functional changes.

Thank you for the simplification.

Tyler

Quoted 38 lines
---
 fs/ecryptfs/crypto.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
fs/ecryptfs/crypto.c +4 -14
index d49cdf7292ab..82fc5e1b6324 100644
@@ -1422,21 +1422,11 @@ ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
 static int ecryptfs_copy_filename(char **copied_name, size_t *copied_name_size,
 				  const char *name, size_t name_size)
 {
-	int rc = 0;
-
-	(*copied_name) = kmalloc((name_size + 1), GFP_KERNEL);
-	if (!(*copied_name)) {
-		rc = -ENOMEM;
-		goto out;
-	}
-	memcpy((void *)(*copied_name), (void *)name, name_size);
-	(*copied_name)[(name_size)] = '\0';	/* Only for convenience
-						 * in printing out the
-						 * string in debug
-						 * messages */
+	(*copied_name) = kmemdup_nul(name, name_size, GFP_KERNEL);
+	if (!(*copied_name))
+		return -ENOMEM;
 	(*copied_name_size) = name_size;
-out:
-	return rc;
+	return 0;
 }
 
 /**
Signature · 2 lines
Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4