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 <rpm@xenomai.org>
parent 9108b0df
...@@ -248,7 +248,7 @@ int main(int argc, char *argv[]) ...@@ -248,7 +248,7 @@ int main(int argc, char *argv[])
return -1; 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); printf("RX rxsock=%d, ifr_name=%s\n", rxsock, ifr.ifr_name);
if (ioctl(rxsock, SIOCGIFINDEX, &ifr) < 0) { if (ioctl(rxsock, SIOCGIFINDEX, &ifr) < 0) {
...@@ -282,7 +282,7 @@ int main(int argc, char *argv[]) ...@@ -282,7 +282,7 @@ int main(int argc, char *argv[])
goto failure1; 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); printf("TX txsock=%d, ifr_name=%s\n", txsock, ifr.ifr_name);
if (ioctl(txsock, SIOCGIFINDEX, &ifr) < 0) { if (ioctl(txsock, SIOCGIFINDEX, &ifr) < 0) {
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include <net/if.h> #include <net/if.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/ether.h> #include <netinet/ether.h>
#include <boilerplate/ancillaries.h>
char buffer[10*1024]; char buffer[10*1024];
int sock; int sock;
...@@ -72,7 +73,7 @@ int main(int argc, char *argv[]) ...@@ -72,7 +73,7 @@ int main(int argc, char *argv[])
if (argc > 1) { if (argc > 1) {
struct ifreq ifr; struct ifreq ifr;
strncpy(ifr.ifr_name, argv[1], IFNAMSIZ); namecpy(ifr.ifr_name, argv[1]);
if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) { if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
perror("cannot get interface index"); perror("cannot get interface index");
close(sock); close(sock);
......
...@@ -1353,7 +1353,7 @@ static void process_options (int argc, char *argv[], int max_cpus) ...@@ -1353,7 +1353,7 @@ static void process_options (int argc, char *argv[], int max_cpus)
case 'F': case 'F':
case OPT_FIFO: case OPT_FIFO:
use_fifo = 1; use_fifo = 1;
strncpy(fifopath, optarg, strlen(optarg)); strncpy(fifopath, optarg, sizeof(fifopath) - 1);
break; break;
case 'H': case 'H':
...@@ -1458,7 +1458,7 @@ static void process_options (int argc, char *argv[], int max_cpus) ...@@ -1458,7 +1458,7 @@ static void process_options (int argc, char *argv[], int max_cpus)
case 'T': case 'T':
case OPT_TRACER: case OPT_TRACER:
tracetype = CUSTOM; tracetype = CUSTOM;
strncpy(tracer, optarg, sizeof(tracer)); strncpy(tracer, optarg, sizeof(tracer) - 1);
break; break;
case 'u': case 'u':
case OPT_UNBUFFERED: case OPT_UNBUFFERED:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment