From 87103d95678b0e6c784909e0bc610ed7ae02d2c9 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 7 Apr 2015 13:56:13 +0200 Subject: netsniff-ng: Explicitely NULL-terminate readlink() result buffer Commit 6c5d0caf3b7c ("netsniff-ng: Fix process name when sniffing nlmon device") fixed the problem of not NULL-terminating the readlink() result buffer by initializing the entire buffer with '\0'. Switch to the more common and better readable idiom of explicitely writing a NULL byte after the readlink result string to make this more obvious. Also change the buffer size to PATH_MAX. Signed-off-by: Tobias Klauser --- proto_nlmsg.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'proto_nlmsg.c') diff --git a/proto_nlmsg.c b/proto_nlmsg.c index 157ea58..6f3d310 100644 --- a/proto_nlmsg.c +++ b/proto_nlmsg.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -18,7 +19,7 @@ static void nlmsg(struct pkt_buff *pkt) struct nlmsghdr *hdr = (struct nlmsghdr *) pkt_pull(pkt, sizeof(*hdr)); char type[32]; char flags[128]; - char procname[1024] = {}; + char procname[PATH_MAX]; if (hdr == NULL) return; @@ -37,7 +38,8 @@ static void nlmsg(struct pkt_buff *pkt) snprintf(path, sizeof(path), "/proc/%u/exe", hdr->nlmsg_pid); ret = readlink(path, procname, sizeof(procname) - 1); if (ret < 0) - procname[0] = '\0'; + ret = 0; + procname[ret] = '\0'; } else snprintf(procname, sizeof(procname), "kernel"); -- cgit v1.2.3-54-g00ecf