From 014bb3f18fb8c97c9da55eeb384968bc383bfc55 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Fri, 13 Jun 2014 15:47:31 -0700 Subject: [PATCH] Use PATH_MAX as buffer size for buffers containing paths. Docker-DCO-1.1-Signed-off-by: Mrunal Patel (github: mrunalp) --- namespaces/nsenter.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/namespaces/nsenter.go b/namespaces/nsenter.go index d5c2e761b..368294eeb 100644 --- a/namespaces/nsenter.go +++ b/namespaces/nsenter.go @@ -4,6 +4,7 @@ package namespaces #include #include #include +#include #include #include #include @@ -73,12 +74,9 @@ void nsenter() { argv += 3; // Setns on all supported namespaces. - char ns_dir[kBufSize]; - memset(ns_dir, 0, kBufSize); - if (snprintf(ns_dir, kBufSize - 1, "/proc/%d/ns/", init_pid) < 0) { - fprintf(stderr, "nsenter: Error getting ns dir path with error: \"%s\"\n", strerror(errno)); - exit(1); - } + char ns_dir[PATH_MAX]; + memset(ns_dir, 0, PATH_MAX); + snprintf(ns_dir, PATH_MAX - 1, "/proc/%d/ns/", init_pid); struct dirent *dent; DIR *dir = opendir(ns_dir); if (dir == NULL) { @@ -92,10 +90,9 @@ void nsenter() { } // Get and open the namespace for the init we are joining.. - char buf[kBufSize]; - memset(buf, 0, kBufSize); - strncat(buf, ns_dir, kBufSize - 1); - strncat(buf, dent->d_name, kBufSize - 1); + char buf[PATH_MAX]; + memset(buf, 0, PATH_MAX); + snprintf(buf, PATH_MAX - 1, "%s%s", ns_dir, dent->d_name); int fd = open(buf, O_RDONLY); if (fd == -1) { fprintf(stderr, "nsenter: Failed to open ns file \"%s\" for ns \"%s\" with error: \"%s\"\n", buf, dent->d_name, strerror(errno));