linux-nfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/2] SUNRPC: Optimize 'svc_print_xprts()'
@ 2020-03-25  7:04 Christophe JAILLET
  2020-03-25 14:52 ` Chuck Lever
  0 siblings, 1 reply; 5+ messages in thread
From: Christophe JAILLET @ 2020-03-25  7:04 UTC (permalink / raw)
  To: trond.myklebust, anna.schumaker, bfields, chuck.lever, davem,
	kuba, gnb, neilb, tom
  Cc: linux-nfs, linux-kernel, kernel-janitors, Christophe JAILLET

Using 'snprintf' is safer than 'sprintf' because it can avoid a buffer
overflow.
The return value can also be used to avoid a strlen a call.

Finally, we know where we need to copy and the length to copy, so, we
can save a few cycles by rearraging the code and using a memcpy instead of
a strcat.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
This patch should have no functionnal change.
We could go further, use scnprintf and write directly in the destination
buffer. However, this could lead to a truncated last line.
---
 net/sunrpc/svc_xprt.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/net/sunrpc/svc_xprt.c b/net/sunrpc/svc_xprt.c
index df39e7b8b06c..6df861650040 100644
--- a/net/sunrpc/svc_xprt.c
+++ b/net/sunrpc/svc_xprt.c
@@ -118,12 +118,12 @@ int svc_print_xprts(char *buf, int maxlen)
 	list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
 		int slen;
 
-		sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
-		slen = strlen(tmpstr);
-		if (len + slen >= maxlen)
+		slen = snprintf(tmpstr, sizeof(tmpstr), "%s %d\n",
+				xcl->xcl_name, xcl->xcl_max_payload);
+		if (slen >= sizeof(tmpstr) || len + slen >= maxlen)
 			break;
+		memcpy(buf + len, tmpstr, slen + 1);
 		len += slen;
-		strcat(buf, tmpstr);
 	}
 	spin_unlock(&svc_xprt_class_lock);
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-03-26 22:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-25  7:04 [PATCH 2/2] SUNRPC: Optimize 'svc_print_xprts()' Christophe JAILLET
2020-03-25 14:52 ` Chuck Lever
     [not found]   ` <42afbf1f-19e1-a05c-e70c-1d46eaba3a71@wanadoo.fr>
2020-03-25 22:53     ` NeilBrown
     [not found]       ` <2e2d1293-c978-3f1d-5a1e-dc43dc2ad06b@wanadoo.fr>
2020-03-26 21:44         ` NeilBrown
2020-03-26 22:29           ` Chuck Lever

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).