Fix copying of device name to ifreq.

Don't overwrite the last NUL with strncpy() and also replace strcpy()
with strncpy().

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
master
Miroslav Lichvar 2014-09-17 11:11:15 +02:00 committed by Richard Cochran
parent 3760f8b653
commit 9ddd2a6024
2 changed files with 4 additions and 4 deletions

View File

@ -131,7 +131,7 @@ int main(int argc, char *argv[])
memset(&ifreq, 0, sizeof(ifreq));
memset(&cfg, 0, sizeof(cfg));
strncpy(ifreq.ifr_name, device, sizeof(ifreq.ifr_name));
strncpy(ifreq.ifr_name, device, sizeof(ifreq.ifr_name) - 1);
ifreq.ifr_data = (void *) &cfg;

6
sk.c
View File

@ -51,7 +51,7 @@ static int hwts_init(int fd, const char *device, int rx_filter, int one_step)
memset(&ifreq, 0, sizeof(ifreq));
memset(&cfg, 0, sizeof(cfg));
strncpy(ifreq.ifr_name, device, sizeof(ifreq.ifr_name));
strncpy(ifreq.ifr_name, device, sizeof(ifreq.ifr_name) - 1);
ifreq.ifr_data = (void *) &cfg;
cfg.tx_type = one_step ? HWTSTAMP_TX_ONESTEP_SYNC : HWTSTAMP_TX_ON;
@ -85,7 +85,7 @@ int sk_interface_index(int fd, const char *name)
int err;
memset(&ifreq, 0, sizeof(ifreq));
strcpy(ifreq.ifr_name, name);
strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name) - 1);
err = ioctl(fd, SIOCGIFINDEX, &ifreq);
if (err < 0) {
pr_err("ioctl SIOCGIFINDEX failed: %m");
@ -154,7 +154,7 @@ int sk_interface_macaddr(const char *name, struct address *mac)
int err, fd;
memset(&ifreq, 0, sizeof(ifreq));
strcpy(ifreq.ifr_name, name);
strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name) - 1);
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (fd < 0) {