Linux Netfilter development
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@vger.kernel.org
Subject: [nft PATCH 2/2] nfnl_osf: Silence string truncation gcc warnings
Date: Sat, 20 Jul 2019 20:52:26 +0200	[thread overview]
Message-ID: <20190720185226.8876-2-phil@nwl.cc> (raw)
In-Reply-To: <20190720185226.8876-1-phil@nwl.cc>

Albeit a bit too enthusiastic, gcc is right in that these strings may be
truncated since the destination buffer is smaller than the source one.
Get rid of the warnings (and the potential problem) by specifying a
string "precision" of one character less than the destination. This
ensures a terminating nul-character may be written as well.

Fixes: af00174af3ef4 ("src: osf: import nfnl_osf.c to load osf fingerprints")
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 src/nfnl_osf.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/nfnl_osf.c b/src/nfnl_osf.c
index be3fd8100b665..bed9ba64b65c6 100644
--- a/src/nfnl_osf.c
+++ b/src/nfnl_osf.c
@@ -289,32 +289,34 @@ static int osf_load_line(char *buffer, int len, int del,
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
-		cnt = snprintf(obuf, sizeof(obuf), "%s,", pbeg);
+		i = sizeof(obuf);
+		cnt = snprintf(obuf, i, "%.*s,", i - 2, pbeg);
 		pbeg = pend + 1;
 	}
 
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
+		i = sizeof(f.genre);
 		if (pbeg[0] == '@' || pbeg[0] == '*')
-			cnt = snprintf(f.genre, sizeof(f.genre), "%s", pbeg + 1);
-		else
-			cnt = snprintf(f.genre, sizeof(f.genre), "%s", pbeg);
+			pbeg++;
+		cnt = snprintf(f.genre, i, "%.*s", i - 1, pbeg + 1);
 		pbeg = pend + 1;
 	}
 
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
-		cnt = snprintf(f.version, sizeof(f.version), "%s", pbeg);
+		i = sizeof(f.version);
+		cnt = snprintf(f.version, i, "%.*s", i - 1, pbeg);
 		pbeg = pend + 1;
 	}
 
 	pend = nf_osf_strchr(pbeg, OSFPDEL);
 	if (pend) {
 		*pend = '\0';
-		cnt =
-		    snprintf(f.subtype, sizeof(f.subtype), "%s", pbeg);
+		i = sizeof(f.subtype);
+		cnt = snprintf(f.subtype, i, "%.*s", i - 1, pbeg);
 		pbeg = pend + 1;
 	}
 
-- 
2.22.0


  reply	other threads:[~2019-07-20 18:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-20 18:52 [nft PATCH 1/2] parser_bison: Get rid of (most) bison compiler warnings Phil Sutter
2019-07-20 18:52 ` Phil Sutter [this message]
2019-07-20 19:36   ` [nft PATCH 2/2] nfnl_osf: Silence string truncation gcc warnings Florian Westphal
2019-07-20 20:22     ` Phil Sutter
2019-07-21 11:15   ` Fernando Fernandez Mancera
2019-07-21 11:29     ` Phil Sutter
2019-07-20 19:43 ` [nft PATCH 1/2] parser_bison: Get rid of (most) bison compiler warnings Florian Westphal
2019-07-20 20:13   ` Phil Sutter

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=20190720185226.8876-2-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.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