netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Stephen Hemminger <stephen@networkplumber.org>
Cc: netdev@vger.kernel.org
Subject: [iproute PATCH] utils: Review strlcpy() and strlcat()
Date: Wed,  6 Sep 2017 18:51:42 +0200	[thread overview]
Message-ID: <20170906165142.28599-1-phil@nwl.cc> (raw)
In-Reply-To: <063D6719AE5E284EB5DD2968C1650D6DD006EC26@AcuExch.aculab.com>

As David Laight correctly pointed out, the first version of strlcpy()
modified dst buffer behind the string copied into it. Fix this by
writing NUL to the byte immediately following src string instead of to
the last byte in dst. Doing so also allows to reduce overhead by using
memcpy().

Improve strlcat() by avoiding the call to strlcpy() if dst string is
already full, not just as sanity check.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 lib/utils.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/lib/utils.c b/lib/utils.c
index 330ab073c2068..bbd3cbc46a0e5 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1233,18 +1233,22 @@ int get_real_family(int rtm_type, int rtm_family)
 
 size_t strlcpy(char *dst, const char *src, size_t size)
 {
+	size_t srclen = strlen(src);
+
 	if (size) {
-		strncpy(dst, src, size - 1);
-		dst[size - 1] = '\0';
+		size_t minlen = min(srclen, size - 1);
+
+		memcpy(dst, src, minlen);
+		dst[minlen] = '\0';
 	}
-	return strlen(src);
+	return srclen;
 }
 
 size_t strlcat(char *dst, const char *src, size_t size)
 {
 	size_t dlen = strlen(dst);
 
-	if (dlen > size)
+	if (dlen >= size)
 		return dlen + strlen(src);
 
 	return dlen + strlcpy(dst + dlen, src, size - dlen);
-- 
2.13.1

  parent reply	other threads:[~2017-09-06 16:51 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-01 16:52 [iproute PATCH 0/6] strlcpy() and strlcat() for iproute2 Phil Sutter
2017-09-01 16:52 ` [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat() Phil Sutter
2017-09-04 14:49   ` David Laight
2017-09-04 15:00     ` Phil Sutter
2017-09-04 18:25       ` Stephen Hemminger
2017-09-06 13:59         ` David Laight
2017-09-06 15:25           ` Stephen Hemminger
2017-09-06 16:51           ` Phil Sutter [this message]
2017-09-01 16:52 ` [iproute PATCH 2/6] Convert the obvious cases to strlcpy() Phil Sutter
2017-09-01 19:13   ` Daniel Borkmann
2017-09-01 16:52 ` [iproute PATCH 3/6] Convert harmful calls to strncpy() " Phil Sutter
2017-09-01 16:52 ` [iproute PATCH 4/6] ipxfrm: Replace STRBUF_CAT macro with strlcat() Phil Sutter
2017-09-01 16:52 ` [iproute PATCH 5/6] tc_util: No need to terminate an snprintf'ed buffer Phil Sutter
2017-09-01 16:52 ` [iproute PATCH 6/6] lnstat_util: Make sure buffer is NUL-terminated Phil Sutter
2017-09-01 19:12 ` [iproute PATCH 0/6] strlcpy() and strlcat() for iproute2 Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170906165142.28599-1-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).