Commit cdc63fe2 authored by Philippe Gerum's avatar Philippe Gerum

demos/posix: prevent unterminated destination buffer with strncpy()

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.
Signed-off-by: Philippe Gerum's avatarPhilippe Gerum <>
parent 9108b0df
......@@ -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) {
......@@ -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");
......@@ -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);
case 'H':
......@@ -1458,7 +1458,7 @@ static void process_options (int argc, char *argv[], int max_cpus)
case 'T':
tracetype = CUSTOM;
strncpy(tracer, optarg, sizeof(tracer));
strncpy(tracer, optarg, sizeof(tracer) - 1);
case 'u':
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment