From: Kim Jones Date: Wed, 17 Aug 2016 09:08:27 +0000 (+0100) Subject: hugepages: check for error if readdir returns NULL X-Git-Tag: collectd-5.7.0~119^2 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=51b24f4c2a15f3f97c2989a36be82e18843b449c;p=collectd.git hugepages: check for error if readdir returns NULL Change-Id: Ib361b648407c7052218c1f6b9b624cb7aa799634 Signed-off-by: Kim Jones --- diff --git a/src/hugepages.c b/src/hugepages.c index 120dd798..ac9441d0 100644 --- a/src/hugepages.c +++ b/src/hugepages.c @@ -187,6 +187,7 @@ static int read_syshugepages(const char* path, const char* node) while ((result = readdir(dir)) != NULL) { if (strncmp(result->d_name, hugepages_dir, sizeof(hugepages_dir)-1)) { /* not node dir */ + errno = 0; continue; } @@ -196,10 +197,17 @@ static int read_syshugepages(const char* path, const char* node) e_info.d_name = result->d_name; e_info.node = node; walk_directory(path2, read_hugepage_entry, &e_info, 0); + errno = 0; } - closedir(dir); + /* Check if NULL return from readdir() was an error */ + if (errno != 0) { + ERROR("%s: readdir failed", g_plugin_name); + closedir(dir); + return -1; + } + closedir(dir); return 0; } @@ -234,15 +242,23 @@ static int read_nodes(void) while ((result = readdir(dir)) != NULL) { if (strncmp(result->d_name, node_string, sizeof(node_string)-1)) { /* not node dir */ + errno = 0; continue; } ssnprintf(path, (size_t) lim, sys_node_hugepages, result->d_name); read_syshugepages(path, result->d_name); + errno = 0; } - closedir(dir); + /* Check if NULL return from readdir() was an error */ + if (errno != 0) { + ERROR("%s: readdir failed", g_plugin_name); + closedir(dir); + return -1; + } + closedir(dir); return 0; }