Skip to contentSkip to conversation

nfsd: remove private bin2hex implementation

PATCH In review Andy Shevchenko 2 participants 2 messages Oct 11, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfsd: remove private bin2hex implementation

Calling sprintf in a loop is not very efficient, and in any case, we already have an implementation of bin-to-hex conversion in lib/ which we might as well use.

Note that original code used to nul-terminate the destination while bin2hex doesn't. That's why replace kmalloc() with kzalloc().

---
 fs/nfsd/nfs4recover.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
fs/nfsd/nfs4recover.c +3 -8
index cdc75ad4438b..29dff4c6e752 100644
@@ -1850,19 +1850,14 @@ nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1)
 static char *
 bin_to_hex_dup(const unsigned char *src, int srclen)
 {
-	int i;
-	char *buf, *hex;
+	char *buf;
 
 	/* +1 for terminating NULL */
-	buf = kmalloc((srclen * 2) + 1, GFP_KERNEL);
+	buf = kzalloc((srclen * 2) + 1, GFP_KERNEL);
 	if (!buf)
 		return buf;
 
-	hex = buf;
-	for (i = 0; i < srclen; i++) {
-		sprintf(hex, "%2.2x", *src++);
-		hex += 2;
-	}
+	bin2hex(buf, src, srclen);
 	return buf;
 }
 
Signature · 1 line
2.23.0
#1 lore raw

Thanks, applying.--b.

Quoted 41 lines · Andy Shevchenko

Calling sprintf in a loop is not very efficient, and in any case, we already have an implementation of bin-to-hex conversion in lib/ which we might as well use.

Note that original code used to nul-terminate the destination while bin2hex doesn't. That's why replace kmalloc() with kzalloc().

---
 fs/nfsd/nfs4recover.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
fs/nfsd/nfs4recover.c +3 -8
index cdc75ad4438b..29dff4c6e752 100644
@@ -1850,19 +1850,14 @@ nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1)
 static char *
 bin_to_hex_dup(const unsigned char *src, int srclen)
 {
-	int i;
-	char *buf, *hex;
+	char *buf;
 
 	/* +1 for terminating NULL */
-	buf = kmalloc((srclen * 2) + 1, GFP_KERNEL);
+	buf = kzalloc((srclen * 2) + 1, GFP_KERNEL);
 	if (!buf)
 		return buf;
 
-	hex = buf;
-	for (i = 0; i < srclen; i++) {
-		sprintf(hex, "%2.2x", *src++);
-		hex += 2;
-	}
+	bin2hex(buf, src, srclen);
 	return buf;
 }
 
Signature · 1 line
2.23.0