From dca1bd6aaaa8aebf557071275e77140f91871c0d Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Tue, 20 Sep 2016 10:00:14 +0800 Subject: [PATCH] ensure the read buf is null-terminated Signed-off-by: Lai Jiangshan --- src/net.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/net.c b/src/net.c index e6d890e..ef00bda 100644 --- a/src/net.c +++ b/src/net.c @@ -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; }