All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philippe Gerum <rpm@xenomai.org>
To: xenomai@xenomai.org
Cc: Jan Kiszka <jan.kiszka@siemens.com>, Philippe Gerum <rpm@xenomai.org>
Subject: [PATCH 2/9] demos/posix: prevent unterminated destination buffer with strncpy()
Date: Fri, 26 Oct 2018 16:08:18 +0200	[thread overview]
Message-ID: <20181026140825.5251-2-rpm@xenomai.org> (raw)
In-Reply-To: <20181026140825.5251-1-rpm@xenomai.org>

GCC 8.x introduced -Wstringop-truncation to help detecting likely
unwanted outcomes of strncpy(dst, src, n), such as omitting the NUL
character into the destination buffer whenever n < sizeof(src).

Fix unsafe strncpy() calls when we do expect a null-terminated
destination buffer.
---
 demo/posix/cobalt/can-rtt.c        | 4 ++--
 demo/posix/cobalt/eth_p_all.c      | 3 ++-
 demo/posix/cyclictest/cyclictest.c | 4 ++--
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/demo/posix/cobalt/can-rtt.c b/demo/posix/cobalt/can-rtt.c
index 61cad05e5..dd212d804 100644
--- a/demo/posix/cobalt/can-rtt.c
+++ b/demo/posix/cobalt/can-rtt.c
@@ -248,7 +248,7 @@ int main(int argc, char *argv[])
 	return -1;
     }
 
-    strncpy(ifr.ifr_name, rxdev, IFNAMSIZ);
+    namecpy(ifr.ifr_name, rxdev);
     printf("RX rxsock=%d, ifr_name=%s\n", rxsock, ifr.ifr_name);
 
     if (ioctl(rxsock, SIOCGIFINDEX, &ifr) < 0) {
@@ -282,7 +282,7 @@ int main(int argc, char *argv[])
 	    goto failure1;
 	}
 
-	strncpy(ifr.ifr_name, txdev, IFNAMSIZ);
+	namecpy(ifr.ifr_name, txdev);
 	printf("TX txsock=%d, ifr_name=%s\n", txsock, ifr.ifr_name);
 
 	if (ioctl(txsock, SIOCGIFINDEX, &ifr) < 0) {
diff --git a/demo/posix/cobalt/eth_p_all.c b/demo/posix/cobalt/eth_p_all.c
index 6ac12ab3e..91aef9fbd 100644
--- a/demo/posix/cobalt/eth_p_all.c
+++ b/demo/posix/cobalt/eth_p_all.c
@@ -40,6 +40,7 @@
 #include <net/if.h>
 #include <arpa/inet.h>
 #include <netinet/ether.h>
+#include <boilerplate/ancillaries.h>
 
 char buffer[10*1024];
 int sock;
@@ -72,7 +73,7 @@ int main(int argc, char *argv[])
 	if (argc > 1) {
 		struct ifreq ifr;
 
-		strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
+		namecpy(ifr.ifr_name, argv[1]);
 		if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
 			perror("cannot get interface index");
 			close(sock);
diff --git a/demo/posix/cyclictest/cyclictest.c b/demo/posix/cyclictest/cyclictest.c
index ebe5461db..76983bd02 100644
--- a/demo/posix/cyclictest/cyclictest.c
+++ b/demo/posix/cyclictest/cyclictest.c
@@ -1353,7 +1353,7 @@ static void process_options (int argc, char *argv[], int max_cpus)
 		case 'F':
 		case OPT_FIFO:
 			use_fifo = 1;
-			strncpy(fifopath, optarg, strlen(optarg));
+			strncpy(fifopath, optarg, sizeof(fifopath) - 1);
 			break;
 
 		case 'H':
@@ -1458,7 +1458,7 @@ static void process_options (int argc, char *argv[], int max_cpus)
 		case 'T':
 		case OPT_TRACER:
 			tracetype = CUSTOM;
-			strncpy(tracer, optarg, sizeof(tracer));
+			strncpy(tracer, optarg, sizeof(tracer) - 1);
 			break;
 		case 'u':
 		case OPT_UNBUFFERED:
-- 
2.17.2



  reply	other threads:[~2018-10-26 14:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-26 14:08 [PATCH 1/9] utils/can: prevent unterminated destination buffer with strncpy() Philippe Gerum
2018-10-26 14:08 ` Philippe Gerum [this message]
2018-10-26 14:08 ` [PATCH 3/9] boilerplate/ancillaries: prevent false positive with -Wstringop-truncation Philippe Gerum
2018-10-26 14:08 ` [PATCH 4/9] cobalt/ancillaries: " Philippe Gerum
2019-01-24 18:35   ` Jan Kiszka
2018-10-26 14:08 ` [PATCH 5/9] boilerplate/compiler: drop __const and __pure shorthands Philippe Gerum
2018-10-26 14:08 ` [PATCH 6/9] net/stack: export services to switch interface up/down Philippe Gerum
2018-10-26 14:08 ` [PATCH 7/9] net/stack: ignore extraneous interface UP/DOWN calls Philippe Gerum
2018-10-26 14:08 ` [PATCH 8/9] net/igb: down interface upon PCI unregister Philippe Gerum
2018-10-31 12:32   ` Jan Kiszka
2018-10-31 14:18     ` Philippe Gerum
2018-10-31 15:10       ` Jan Kiszka
2018-10-31 16:32         ` Philippe Gerum
2018-10-31 16:40           ` Jan Kiszka
2018-10-26 14:08 ` [PATCH 9/9] net/igb: igb_ioctl() requires execution in secondary mode Philippe Gerum

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=20181026140825.5251-2-rpm@xenomai.org \
    --to=rpm@xenomai.org \
    --cc=jan.kiszka@siemens.com \
    --cc=xenomai@xenomai.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.