Skip to contentSkip to conversation

nfsd: Replace open-coded conversion of bytes to hex

PATCH Superseded Eric Biggers 3 participants 4 messages Aug 3, 2025 – Aug 4, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfsd: Replace open-coded conversion of bytes to hex

Since the Linux kernel's sprintf() has conversion to hex built-in via "%*phN", delete md5_to_hex() and just use that. Also add an explicit array bound to the dname parameter of nfs4_make_rec_clidname() to make its size clear. No functional change.

---
 fs/nfsd/nfs4recover.c | 18 ++----------------
 1 file changed, 2 insertions(+), 16 deletions(-)
fs/nfsd/nfs4recover.c +2 -16
index 2231192ec33f5..54f5e5392ef9d 100644
@@ -90,26 +90,12 @@ static void
 nfs4_reset_creds(const struct cred *original)
 {
 	put_cred(revert_creds(original));
 }
 
-static void
-md5_to_hex(char *out, char *md5)
-{
-	int i;
-
-	for (i=0; i<16; i++) {
-		unsigned char c = md5[i];
-
-		*out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
-		*out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
-	}
-	*out = '\0';
-}
-
 static int
-nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
+nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
 {
 	struct xdr_netobj cksum;
 	struct crypto_shash *tfm;
 	int status;
 
@@ -131,11 +117,11 @@ nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
 	status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
 					 cksum.data);
 	if (status)
 		goto out;
 
-	md5_to_hex(dname, cksum.data);
+	sprintf(dname, "%*phN", 16, cksum.data);
 
 	status = 0;
 out:
 	kfree(cksum.data);
 	crypto_free_shash(tfm);

base-commit: 186f3edfdd41f2ae87fc40a9ccba52a3bf930994

Signature · 1 line
2.50.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 56 lines · Eric Biggers

Since the Linux kernel's sprintf() has conversion to hex built-in via "%*phN", delete md5_to_hex() and just use that. Also add an explicit array bound to the dname parameter of nfs4_make_rec_clidname() to make its size clear. No functional change.

---
 fs/nfsd/nfs4recover.c | 18 ++----------------
 1 file changed, 2 insertions(+), 16 deletions(-)
fs/nfsd/nfs4recover.c +2 -16
index 2231192ec33f5..54f5e5392ef9d 100644
@@ -90,26 +90,12 @@ static void
 nfs4_reset_creds(const struct cred *original)
 {
 	put_cred(revert_creds(original));
 }
 
-static void
-md5_to_hex(char *out, char *md5)
-{
-	int i;
-
-	for (i=0; i<16; i++) {
-		unsigned char c = md5[i];
-
-		*out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
-		*out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
-	}
-	*out = '\0';
-}
-
 static int
-nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
+nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
 {
 	struct xdr_netobj cksum;
 	struct crypto_shash *tfm;
 	int status;
 
@@ -131,11 +117,11 @@ nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
 	status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
 					 cksum.data);
 	if (status)
 		goto out;
 
-	md5_to_hex(dname, cksum.data);
+	sprintf(dname, "%*phN", 16, cksum.data);
 
 	status = 0;
 out:
 	kfree(cksum.data);
 	crypto_free_shash(tfm);
Quoted 1 line

base-commit: 186f3edfdd41f2ae87fc40a9ccba52a3bf930994

#2 lore raw
In reply to Eric Biggers
Quoted 43 lines · Eric Biggers

Since the Linux kernel's sprintf() has conversion to hex built-in via "%*phN", delete md5_to_hex() and just use that. Also add an explicit array bound to the dname parameter of nfs4_make_rec_clidname() to make its size clear. No functional change.

---
 fs/nfsd/nfs4recover.c | 18 ++----------------
 1 file changed, 2 insertions(+), 16 deletions(-)
fs/nfsd/nfs4recover.c +1 -15
index 2231192ec33f5..54f5e5392ef9d 100644
@@ -90,26 +90,12 @@ static void
 nfs4_reset_creds(const struct cred *original)
 {
 	put_cred(revert_creds(original));
 }
 
-static void
-md5_to_hex(char *out, char *md5)
-{
-	int i;
-
-	for (i=0; i<16; i++) {
-		unsigned char c = md5[i];
-
-		*out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
-		*out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
-	}
-	*out = '\0';
-}
-
 static int
-nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
+nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
 {
 	struct xdr_netobj cksum;
 	struct crypto_shash *tfm;
 	int status;
 

fs/nfsd/nfs4recover.c nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname) · line 122

Quoted 7 lines
fs/nfsd/nfs4recover.c +1 -1
@@ -131,11 +117,11 @@ nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
 	status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
 					 cksum.data);
 	if (status)
 		goto out;
 
-	md5_to_hex(dname, cksum.data);
+	sprintf(dname, "%*phN", 16, cksum.data);

Hello Eric,

Can the raw "16" be replaced with "HEXDIR_LEN / 2" ?

Quoted 6 lines
fs/nfsd/nfs4recover.c +0 -0
 	status = 0;
 out:
 	kfree(cksum.data);
 	crypto_free_shash(tfm);
 

base-commit: 186f3edfdd41f2ae87fc40a9ccba52a3bf930994

Signature · 1 line
Chuck Lever
#3 lore raw
Quoted 49 lines · Chuck Lever
Quoted 48 lines · Eric Biggers

Since the Linux kernel's sprintf() has conversion to hex built-in via "%*phN", delete md5_to_hex() and just use that. Also add an explicit array bound to the dname parameter of nfs4_make_rec_clidname() to make its size clear. No functional change.

---
 fs/nfsd/nfs4recover.c | 18 ++----------------
 1 file changed, 2 insertions(+), 16 deletions(-)
fs/nfsd/nfs4recover.c +1 -15
index 2231192ec33f5..54f5e5392ef9d 100644
@@ -90,26 +90,12 @@ static void
 nfs4_reset_creds(const struct cred *original)
 {
 	put_cred(revert_creds(original));
 }
 
-static void
-md5_to_hex(char *out, char *md5)
-{
-	int i;
-
-	for (i=0; i<16; i++) {
-		unsigned char c = md5[i];
-
-		*out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
-		*out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
-	}
-	*out = '\0';
-}
-
 static int
-nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
+nfs4_make_rec_clidname(char dname[HEXDIR_LEN], const struct xdr_netobj *clname)
 {
 	struct xdr_netobj cksum;
 	struct crypto_shash *tfm;
 	int status;
 
@@ -131,11 +117,11 @@ nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
 	status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
 					 cksum.data);
 	if (status)
 		goto out;
 
Quoted 14 lines

fs/nfsd/nfs4recover.c nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname) · line 122

Quoted 2 lines
fs/nfsd/nfs4recover.c +1 -1
-	md5_to_hex(dname, cksum.data);
+	sprintf(dname, "%*phN", 16, cksum.data);

Hello Eric,

Can the raw "16" be replaced with "HEXDIR_LEN / 2" ?

Quoted 6 lines
fs/nfsd/nfs4recover.c +0 -0
 	status = 0;
 out:
 	kfree(cksum.data);
 	crypto_free_shash(tfm);
 

base-commit: 186f3edfdd41f2ae87fc40a9ccba52a3bf930994

It's the same 16 as was there before. It really should be MD5_DIGEST_SIZE, but there are a few more things to clean up regarding that. I didn't want to bloat the scope of this patch.

I sent a new series with this patch unchanged, but the digest size stuff cleaned up in another patch.

- Eric