Use PATH_MAX as buffer size for buffers containing paths.

Docker-DCO-1.1-Signed-off-by: Mrunal Patel <mrunalp@gmail.com> (github: mrunalp)
This commit is contained in:
Mrunal Patel 2014-06-13 15:47:31 -07:00
parent bfcd86f32d
commit 014bb3f18f
1 changed files with 7 additions and 10 deletions

View File

@ -4,6 +4,7 @@ package namespaces
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <linux/limits.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
@ -73,12 +74,9 @@ void nsenter() {
argv += 3; argv += 3;
// Setns on all supported namespaces. // Setns on all supported namespaces.
char ns_dir[kBufSize]; char ns_dir[PATH_MAX];
memset(ns_dir, 0, kBufSize); memset(ns_dir, 0, PATH_MAX);
if (snprintf(ns_dir, kBufSize - 1, "/proc/%d/ns/", init_pid) < 0) { snprintf(ns_dir, PATH_MAX - 1, "/proc/%d/ns/", init_pid);
fprintf(stderr, "nsenter: Error getting ns dir path with error: \"%s\"\n", strerror(errno));
exit(1);
}
struct dirent *dent; struct dirent *dent;
DIR *dir = opendir(ns_dir); DIR *dir = opendir(ns_dir);
if (dir == NULL) { if (dir == NULL) {
@ -92,10 +90,9 @@ void nsenter() {
} }
// Get and open the namespace for the init we are joining.. // Get and open the namespace for the init we are joining..
char buf[kBufSize]; char buf[PATH_MAX];
memset(buf, 0, kBufSize); memset(buf, 0, PATH_MAX);
strncat(buf, ns_dir, kBufSize - 1); snprintf(buf, PATH_MAX - 1, "%s%s", ns_dir, dent->d_name);
strncat(buf, dent->d_name, kBufSize - 1);
int fd = open(buf, O_RDONLY); int fd = open(buf, O_RDONLY);
if (fd == -1) { 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)); fprintf(stderr, "nsenter: Failed to open ns file \"%s\" for ns \"%s\" with error: \"%s\"\n", buf, dent->d_name, strerror(errno));