Commit 1e665968 authored by Philippe Gerum's avatar Philippe Gerum Committed by Jan Kiszka

utils/can: 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 fbc32710
......@@ -31,6 +31,7 @@
#include <errno.h>
#include <getopt.h>
#include <sys/mman.h>
#include <boilerplate/ancillaries.h>
#include <rtdm/can.h>
......@@ -81,7 +82,7 @@ static int string_to_ctrlmode(char *str)
int main(int argc, char *argv[])
{
char ifname[16];
char ifname[IFNAMSIZ];
int can_fd = -1;
int new_baudrate = -1;
int new_mode = -1;
......@@ -159,8 +160,8 @@ int main(int argc, char *argv[])
return 0;
}
strncpy(ifname, argv[optind], IFNAMSIZ);
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
namecpy(ifname, argv[optind]);
namecpy(ifr.ifr_name, ifname);
if (optind == argc - 2) { /* Get mode setting */
new_mode = string_to_mode(argv[optind + 1]);
......
......@@ -7,6 +7,7 @@
#include <getopt.h>
#include <alchemy/task.h>
#include <boilerplate/ancillaries.h>
#include <rtdm/can.h>
......@@ -248,7 +249,7 @@ int main(int argc, char **argv)
if (verbose)
printf("interface %s\n", argv[optind]);
strncpy(ifr.ifr_name, argv[optind], IFNAMSIZ);
namecpy(ifr.ifr_name, argv[optind]);
if (verbose)
printf("s=%d, ifr_name=%s\n", s, ifr.ifr_name);
......
......@@ -6,6 +6,7 @@
#include <errno.h>
#include <getopt.h>
#include <boilerplate/ancillaries.h>
#include <alchemy/task.h>
#include <alchemy/timer.h>
......@@ -231,7 +232,7 @@ int main(int argc, char **argv)
printf("Using loopback=%d\n", loopback);
}
strncpy(ifr.ifr_name, argv[optind], IFNAMSIZ);
namecpy(ifr.ifr_name, argv[optind]);
if (verbose)
printf("s=%d, ifr_name=%s\n", s, ifr.ifr_name);
......
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