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.

Link: https://github.com/KSPP/linux/issues/370 [1]
Signed-off-by: Ian Bridges <[email protected]>
---
 net/sunrpc/addr.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/net/sunrpc/addr.c b/net/sunrpc/addr.c
index 97ff11973c49..c164ea214e6a 100644
--- a/net/sunrpc/addr.c
+++ b/net/sunrpc/addr.c
@@ -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);
-- 
2.47.3


Reply via email to