slcan tools: fix stringop-truncation warnings

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

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
This commit is contained in:
Oliver Hartkopp
2019-08-25 18:25:35 +02:00
parent 1331c56393
commit 226d6393b8
2 changed files with 17 additions and 13 deletions
+8 -7
View File
@@ -88,7 +88,8 @@ int main(int argc, char **argv)
int send_read_status_flags = 0;
char *speed = NULL;
char *btr = NULL;
char buf[IFNAMSIZ+1];
char buf[20];
static struct ifreq ifr;
char *tty;
char *name = NULL;
int opt;
@@ -133,7 +134,7 @@ int main(int argc, char **argv)
case 'n':
name = optarg;
if (strlen(name) > IFNAMSIZ-1)
if (strlen(name) > sizeof(ifr.ifr_newname) - 1)
print_usage(argv[0]);
break;
@@ -201,16 +202,15 @@ int main(int argc, char **argv)
}
/* 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");
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 */
if (name) {
struct ifreq ifr;
int s = socket(PF_INET, SOCK_DGRAM, 0);
printf("rename netdevice %s to %s ... ", buf, name);
@@ -218,8 +218,9 @@ int main(int argc, char **argv)
if (s < 0)
perror("socket for interface rename");
else {
strncpy (ifr.ifr_name, buf, IFNAMSIZ);
strncpy (ifr.ifr_newname, name, IFNAMSIZ);
/* current slcan%d name is still in ifr.ifr_name */
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)
printf("failed!\n");