From 56a368209ea679eaa6790de419b68573505afb74 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 5 Sep 2016 10:35:22 +0200 Subject: [PATCH] hugepages plugin: Don't use pathconf(_PC_NAME_MAX). Since we allocate the buffer on the stack, this doesn't make sense: Best case, the returned value is the same as the PATH_MAX define. Worst case, the returned value is larger and we create a stack overflow. --- src/hugepages.c | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/src/hugepages.c b/src/hugepages.c index 8573fc34..8e8391f4 100644 --- a/src/hugepages.c +++ b/src/hugepages.c @@ -160,7 +160,6 @@ static int read_syshugepages(const char *path, const char *node) { struct dirent *result; char path2[PATH_MAX]; struct entry_info e_info; - long lim; dir = opendir(path); if (dir == NULL) { @@ -168,18 +167,6 @@ static int read_syshugepages(const char *path, const char *node) { return -1; } - errno = 0; - if ((lim = pathconf(path, _PC_NAME_MAX)) == -1) { - /* Limit not defined if errno == 0, otherwise error */ - if (errno != 0) { - ERROR("%s: pathconf failed", g_plugin_name); - closedir(dir); - return -1; - } else { - lim = PATH_MAX; - } - } - /* read "hugepages-XXXXXkB" entries */ while ((result = readdir(dir)) != NULL) { if (strncmp(result->d_name, hugepages_dir, sizeof(hugepages_dir) - 1)) { @@ -189,7 +176,7 @@ static int read_syshugepages(const char *path, const char *node) { } /* /sys/devices/system/node/node?/hugepages/ */ - ssnprintf(path2, (size_t)lim, "%s/%s", path, result->d_name); + ssnprintf(path2, sizeof(path2), "%s/%s", path, result->d_name); e_info.d_name = result->d_name; e_info.node = node; @@ -216,7 +203,6 @@ static int read_nodes(void) { DIR *dir; struct dirent *result; char path[PATH_MAX]; - long lim; dir = opendir(sys_node); if (dir == NULL) { @@ -224,18 +210,6 @@ static int read_nodes(void) { return -1; } - errno = 0; - if ((lim = pathconf(sys_node, _PC_NAME_MAX)) == -1) { - /* Limit not defined if errno == 0, otherwise error */ - if (errno != 0) { - ERROR("%s: pathconf failed", g_plugin_name); - closedir(dir); - return -1; - } else { - lim = PATH_MAX; - } - } - while ((result = readdir(dir)) != NULL) { if (strncmp(result->d_name, node_string, sizeof(node_string) - 1)) { /* not node dir */ @@ -243,7 +217,7 @@ static int read_nodes(void) { continue; } - ssnprintf(path, (size_t)lim, sys_node_hugepages, result->d_name); + ssnprintf(path, sizeof(path), sys_node_hugepages, result->d_name); read_syshugepages(path, result->d_name); errno = 0; } -- 2.30.2