slcan tools: fix stringop-truncation warnings

Rework string handling for ioctl() syscalls for netdev name operations.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
pull/145/head
Oliver Hartkopp 2019-08-25 18:25:35 +02:00
parent 1331c56393
commit 226d6393b8
2 changed files with 17 additions and 13 deletions

View File

@ -88,7 +88,8 @@ int main(int argc, char **argv)
int send_read_status_flags = 0; int send_read_status_flags = 0;
char *speed = NULL; char *speed = NULL;
char *btr = NULL; char *btr = NULL;
char buf[IFNAMSIZ+1]; char buf[20];
static struct ifreq ifr;
char *tty; char *tty;
char *name = NULL; char *name = NULL;
int opt; int opt;
@ -133,7 +134,7 @@ int main(int argc, char **argv)
case 'n': case 'n':
name = optarg; name = optarg;
if (strlen(name) > IFNAMSIZ-1) if (strlen(name) > sizeof(ifr.ifr_newname) - 1)
print_usage(argv[0]); print_usage(argv[0]);
break; break;
@ -201,16 +202,15 @@ int main(int argc, char **argv)
} }
/* retrieve the name of the created CAN netdevice */ /* retrieve the name of the created CAN netdevice */
if (ioctl (fd, SIOCGIFNAME, buf) < 0) { if (ioctl (fd, SIOCGIFNAME, ifr.ifr_name) < 0) {
perror("ioctl SIOCGIFNAME"); perror("ioctl SIOCGIFNAME");
exit(1); exit(1);
} }
printf("attached tty %s to netdevice %s\n", tty, buf); printf("attached tty %s to netdevice %s\n", tty, ifr.ifr_name);
/* try to rename the created device if requested */ /* try to rename the created device if requested */
if (name) { if (name) {
struct ifreq ifr;
int s = socket(PF_INET, SOCK_DGRAM, 0); int s = socket(PF_INET, SOCK_DGRAM, 0);
printf("rename netdevice %s to %s ... ", buf, name); printf("rename netdevice %s to %s ... ", buf, name);
@ -218,8 +218,9 @@ int main(int argc, char **argv)
if (s < 0) if (s < 0)
perror("socket for interface rename"); perror("socket for interface rename");
else { else {
strncpy (ifr.ifr_name, buf, IFNAMSIZ); /* current slcan%d name is still in ifr.ifr_name */
strncpy (ifr.ifr_newname, name, IFNAMSIZ); memset (ifr.ifr_newname, 0, sizeof(ifr.ifr_newname));
strncpy (ifr.ifr_newname, name, sizeof(ifr.ifr_newname) - 1);
if (ioctl(s, SIOCSIFNAME, &ifr) < 0) if (ioctl(s, SIOCSIFNAME, &ifr) < 0)
printf("failed!\n"); printf("failed!\n");

View File

@ -180,7 +180,8 @@ int main(int argc, char *argv[])
char *tty = NULL; char *tty = NULL;
char const *devprefix = "/dev/"; char const *devprefix = "/dev/";
char *name = NULL; char *name = NULL;
char buf[IFNAMSIZ+1]; char buf[20];
static struct ifreq ifr;
struct termios tios; struct termios tios;
speed_t old_ispeed; speed_t old_ispeed;
speed_t old_ospeed; speed_t old_ospeed;
@ -270,6 +271,8 @@ int main(int argc, char *argv[])
print_usage(argv[0]); print_usage(argv[0]);
name = argv[optind + 1]; name = argv[optind + 1];
if (name && (strlen(name) > sizeof(ifr.ifr_newname) - 1))
print_usage(argv[0]);
/* Prepare the tty device name string */ /* Prepare the tty device name string */
pch = strstr(tty, devprefix); pch = strstr(tty, devprefix);
@ -369,23 +372,23 @@ int main(int argc, char *argv[])
} }
/* retrieve the name of the created CAN netdevice */ /* retrieve the name of the created CAN netdevice */
if (ioctl(fd, SIOCGIFNAME, buf) < 0) { if (ioctl(fd, SIOCGIFNAME, ifr.ifr_name) < 0) {
perror("ioctl SIOCGIFNAME"); perror("ioctl SIOCGIFNAME");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
syslogger(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf); syslogger(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, ifr.ifr_name);
/* try to rename the created netdevice */ /* try to rename the created netdevice */
if (name) { if (name) {
struct ifreq ifr;
int s = socket(PF_INET, SOCK_DGRAM, 0); int s = socket(PF_INET, SOCK_DGRAM, 0);
if (s < 0) if (s < 0)
perror("socket for interface rename"); perror("socket for interface rename");
else { else {
strncpy(ifr.ifr_name, buf, IFNAMSIZ); /* current slcan%d name is still in ifr.ifr_name */
strncpy(ifr.ifr_newname, name, IFNAMSIZ); memset (ifr.ifr_newname, 0, sizeof(ifr.ifr_newname));
strncpy (ifr.ifr_newname, name, sizeof(ifr.ifr_newname) - 1);
if (ioctl(s, SIOCSIFNAME, &ifr) < 0) { if (ioctl(s, SIOCSIFNAME, &ifr) < 0) {
syslogger(LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name); syslogger(LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name);