Skip to contentSkip to conversation

SUNRPC: Replace strlcat() with snprintf() in rpc_sockaddr2uaddr()

PATCH In review Ian Bridges 1 participant 2 messages Jun 27 – Jul 29

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

PATCH 1/1 SUNRPC: Replace strlcat() with snprintf() in rpc_sockaddr2uaddr()

In preparation for removing the deprecated strlcat() API[1], replace the snprintf()/strlcat() pair in rpc_sockaddr2uaddr() with a single snprintf() that appends the port suffix directly to the address buffer.

rpc_ntop4() and rpc_ntop6_noscopeid() leave a NUL-terminated presentation address in addrbuf; the port is then formatted as ".p1.p2" and appended. Writing that suffix at addrbuf + strlen(addrbuf) with snprintf() yields the same universal address string as the separate portbuf/strlcat() step it replaces, so the intermediate portbuf is no longer needed.

snprintf() returns the length it would have written excluding the NUL, so comparing it against the remaining buffer space preserves the existing truncation check that returns NULL.

---
 net/sunrpc/addr.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
net/sunrpc/addr.c +5 -6
index 97ff11973c49..c164ea214e6a 100644
@@ -264,9 +264,9 @@ EXPORT_SYMBOL_GPL(rpc_pton);
  */
 char *rpc_sockaddr2uaddr(const struct sockaddr *sap, gfp_t gfp_flags)
 {
-	char portbuf[RPCBIND_MAXUADDRPLEN];
 	char addrbuf[RPCBIND_MAXUADDRLEN];
 	unsigned short port;
+	size_t len, avail;
 
 	switch (sap->sa_family) {
 	case AF_INET:
@@ -283,11 +283,10 @@ char *rpc_sockaddr2uaddr(const struct sockaddr *sap, gfp_t gfp_flags)
 		return NULL;
 	}
 
-	if (snprintf(portbuf, sizeof(portbuf),
-		     ".%u.%u", port >> 8, port & 0xff) >= (int)sizeof(portbuf))
-		return NULL;
-
-	if (strlcat(addrbuf, portbuf, sizeof(addrbuf)) >= sizeof(addrbuf))
+	len = strlen(addrbuf);
+	avail = sizeof(addrbuf) - len;
+	if (snprintf(addrbuf + len, avail, ".%u.%u",
+		     port >> 8, port & 0xff) >= avail)
 		return NULL;
 
 	return kstrdup(addrbuf, gfp_flags);
Signature · 1 line
2.47.3
#1 lore raw

On Sat, Jun 27, 2026 at 12:11:41AM -0500, Ian Bridges wrote:

Gentle ping. This patch has had no response since it was posted on June 27. The code it converts is unchanged on mainline as of this week, and the patch still applies cleanly. Happy to resend if that is easier.

Thanks,
Ian