ensure the read buf is null-terminated

Signed-off-by: Lai Jiangshan <jiangshanlai@gmail.com>
This commit is contained in:
Lai Jiangshan
2016-09-20 10:00:14 +08:00
parent 7294253548
commit dca1bd6aaa
+2 -2
View File
@@ -179,7 +179,7 @@ static int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int a
static int hyper_get_ifindex(char *nic)
{
int fd, ifindex = -1;
char path[512], buf[4];
char path[512], buf[8];
fprintf(stdout, "net device %s\n", nic);
sprintf(path, "/sys/class/net/%s/ifindex", nic);
@@ -192,7 +192,7 @@ static int hyper_get_ifindex(char *nic)
}
memset(buf, 0, sizeof(buf));
if (read(fd, buf, sizeof(buf)) <= 0) {
if (read(fd, buf, sizeof(buf) - 1) <= 0) {
perror("can read open file");
goto out;
}