From 8681fec1482f379a06267abeb38224aba8068e17 Mon Sep 17 00:00:00 2001 From: Pawel Andruszkiewicz Date: Thu, 11 Jun 2015 10:49:33 +0200 Subject: [PATCH] [Archive] Replaced readdir with readdir_r. Prevent CID: 444374, 447060, 447248 [Verification] TCT pass rate: 100% Change-Id: I3d75b03b1858c4b7af9a1c548b4f48ef7d17327f Signed-off-by: Pawel Andruszkiewicz --- src/archive/filesystem_node.cc | 49 ++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/archive/filesystem_node.cc b/src/archive/filesystem_node.cc index 208955ac..c00907a3 100755 --- a/src/archive/filesystem_node.cc +++ b/src/archive/filesystem_node.cc @@ -222,16 +222,17 @@ PlatformResult Node::getChildNames(Node::NameList* out_name_list) const return PlatformResult(ErrorCode::IO_ERR, "Node has been deleted from platform."); } - errno = 0; - struct dirent *entry = NULL; + int err = 0; + struct dirent entry = {0}; + struct dirent* result = nullptr; NameList name_list; - while ((entry = readdir(dir))) { - if (!strcmp(entry->d_name, ".") || !strncmp(entry->d_name, "..", 2)) { + while ((0 == (err = readdir_r(dir, &entry, &result))) && result) { + if (!strcmp(entry.d_name, ".") || !strncmp(entry.d_name, "..", 2)) { continue; } - name_list.push_back(entry->d_name); + name_list.push_back(entry.d_name); } - if (errno != 0) { + if (0 != err) { LoggerE("throw IOException"); return PlatformResult(ErrorCode::IO_ERR, "Error while reading directory."); } @@ -269,21 +270,22 @@ PlatformResult Node::getChildNodes(NodeList* out_node_list) const return PlatformResult(ErrorCode::IO_ERR, "Node has been deleted from platform."); } - errno = 0; - struct dirent *entry = NULL; + int err = 0; + struct dirent entry = {0}; + struct dirent* result = nullptr; NodeList node_list; - while ((entry = readdir(dir))) { - if (!strcmp(entry->d_name, ".") || !strncmp(entry->d_name, "..", 2)) { + while ((0 == (err = readdir_r(dir, &entry, &result))) && result) { + if (!strcmp(entry.d_name, ".") || !strncmp(entry.d_name, "..", 2)) { continue; } NodePtr node; - Node::resolve(*m_path + entry->d_name, &node); + Node::resolve(*m_path + entry.d_name, &node); node->setPermissions(getPermissions()); // inherit access rights node_list.push_back(node); } - if (errno != 0) { + if (0 != err) { LoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); LoggerE("throw IOException"); return PlatformResult(ErrorCode::IO_ERR, "Error while reading directory."); @@ -596,26 +598,27 @@ PlatformResult Node::removeAsDirectory(const PathPtr& path, bool recursive) LoggerE("throw IOException"); return PlatformResult(ErrorCode::IO_ERR, "Node does not exist or access denied."); } - errno = 0; - struct dirent *entry = NULL; - PlatformResult result(ErrorCode::NO_ERROR); - while ((entry = readdir(dir))) { - if (!strcmp(entry->d_name, ".") || !strncmp(entry->d_name, "..", 2)) { + int err = 0; + struct dirent entry = {0}; + struct dirent* result = nullptr; + PlatformResult platform_result(ErrorCode::NO_ERROR); + while ((0 == (err = readdir_r(dir, &entry, &result))) && result) { + if (!strcmp(entry.d_name, ".") || !strncmp(entry.d_name, "..", 2)) { continue; } - PathPtr subPath = *path + entry->d_name; + PathPtr subPath = *path + entry.d_name; struct stat info; memset(&info, 0, sizeof(struct stat)); if (lstat(subPath->getFullPath().c_str(), &info) == 0) { if (S_ISDIR(info.st_mode)) { - result = removeAsDirectory(subPath, true); + platform_result = removeAsDirectory(subPath, true); } else if (S_ISREG(info.st_mode)) { - result = removeAsFile(subPath); + platform_result = removeAsFile(subPath); } - if (result.error_code() != ErrorCode::NO_ERROR) { - LoggerE("Fail: getFullPath() (%d)",result.error_code()); + if (platform_result.error_code() != ErrorCode::NO_ERROR) { + LoggerE("Fail: getFullPath() (%d)",platform_result.error_code()); closedir(dir); - return result; + return platform_result; } } } -- 2.34.1