Change readdir to readdir_r for thread safety 72/61472/4
authorsangwan.kwon <sangwan.kwon@samsung.com>
Tue, 8 Mar 2016 06:27:48 +0000 (15:27 +0900)
committersangwan.kwon <sangwan.kwon@samsung.com>
Wed, 9 Mar 2016 01:46:27 +0000 (10:46 +0900)
* readdir makes no guarantee of thread safety
* use readdir_r function instead

Change-Id: Id57d0eb33df7bbb41fe8007f543fc75e9d064b01
Signed-off-by: sangwan.kwon <sangwan.kwon@samsung.com>
vcore/vcore/ReferenceValidator.cpp
vcore/vcore/SignatureFinder.cpp

index e8c84b4..72fb404 100644 (file)
@@ -129,72 +129,77 @@ ReferenceValidator::Result ReferenceValidator::Impl::dfsCheckDirectories(
     const std::string &directory,
     bool isAuthorSignature)
 {
-    DIR *dp;
-    struct dirent *dirp;
+    int ret;
+    DIR *dirp;
+    struct dirent entry;
+    struct dirent *result;
+
     std::string currentDir = m_dirpath;
     if (!directory.empty()) {
         currentDir += "/";
         currentDir += directory;
     }
 
-    if ((dp = opendir(currentDir.c_str())) == NULL) {
+    if ((dirp = opendir(currentDir.c_str())) == NULL) {
         LogError("Error opening directory : " << currentDir);
         return ERROR_OPENING_DIR;
     }
 
-    for (errno = 0; (dirp = readdir(dp)) != NULL; errno = 0) {
-        if (!strcmp(dirp->d_name, SPECIAL_SYMBOL_CURRENT_DIR)) {
+    for (ret = readdir_r(dirp, &entry, &result);
+            ret == 0 && result != NULL;
+            ret = readdir_r(dirp, &entry, &result)) {
+        if (!strcmp(result->d_name, SPECIAL_SYMBOL_CURRENT_DIR)) {
             continue;
         }
 
-        if (!strcmp(dirp->d_name, SPECIAL_SYMBOL_UPPER_DIR)) {
+        if (!strcmp(result->d_name, SPECIAL_SYMBOL_UPPER_DIR)) {
             continue;
         }
 
-        if (currentDir == m_dirpath && dirp->d_type == DT_REG &&
-            !strcmp(dirp->d_name, SPECIAL_SYMBOL_AUTHOR_SIGNATURE_FILE) &&
+        if (currentDir == m_dirpath && result->d_type == DT_REG &&
+            !strcmp(result->d_name, SPECIAL_SYMBOL_AUTHOR_SIGNATURE_FILE) &&
             isAuthorSignature)
         {
             continue;
         }
 
-        if (currentDir == m_dirpath && dirp->d_type == DT_REG &&
-            isDistributorSignature(dirp->d_name)) {
+        if (currentDir == m_dirpath && result->d_type == DT_REG &&
+            isDistributorSignature(result->d_name)) {
             continue;
         }
 
-        if (dirp->d_type == DT_DIR) {
-            LogDebug("Open directory : " << (directory + dirp->d_name));
-            std::string tmp_directory = directory + dirp->d_name + "/";
+        if (result->d_type == DT_DIR) {
+            LogDebug("Open directory : " << (directory + result->d_name));
+            std::string tmp_directory = directory + result->d_name + "/";
             Result result = dfsCheckDirectories(referenceSet,
                                                 tmp_directory,
                                                 isAuthorSignature);
             if (result != NO_ERROR) {
-                closedir(dp);
+                closedir(dirp);
                 return result;
             }
-        } else if (dirp->d_type == DT_REG) {
+        } else if (result->d_type == DT_REG) {
             if (referenceSet.end() ==
-                referenceSet.find(directory + dirp->d_name))
+                referenceSet.find(directory + result->d_name))
             {
-                LogDebug("Found file : " << (directory + dirp->d_name));
+                LogDebug("Found file : " << (directory + result->d_name));
                 LogError("Unknown ERROR_REFERENCE_NOT_FOUND.");
-                closedir(dp);
+                closedir(dirp);
                 return ERROR_REFERENCE_NOT_FOUND;
             }
         } else {
             LogError("Unknown file type.");
-            closedir(dp);
+            closedir(dirp);
             return ERROR_UNSUPPORTED_FILE_TYPE;
         }
     }
 
-    if (errno != 0) {
-        closedir(dp);
+    if (ret != 0) {
+        closedir(dirp);
         return ERROR_READING_DIR;
     }
 
-    closedir(dp);
+    closedir(dirp);
 
     return NO_ERROR;
 }
index fd2db17..6d0b2e7 100644 (file)
@@ -70,18 +70,22 @@ std::string SignatureFinder::Impl::getFullPath(const std::string &file)
 
 SignatureFinder::Result SignatureFinder::Impl::find(SignatureFileInfoSet &set)
 {
-    DIR *dp;
-    struct dirent *dirp;
+    int ret;
+    DIR *dirp;
+    struct dirent entry;
+    struct dirent *result;
 
-    if ((dp = opendir(m_dir.c_str())) == NULL) {
+    if ((dirp = opendir(m_dir.c_str())) == NULL) {
         LogError("Error opening directory: " << m_dir);
         return ERROR_OPENING_DIR;
     }
 
-    for (errno = 0; (dirp = readdir(dp)) != NULL; errno = 0) {
+    for (ret = readdir_r(dirp, &entry, &result);
+            ret == 0 && result != NULL;
+            ret = readdir_r(dirp, &entry, &result)) {
         /* number for author signature is -1 */
-        if (!strcmp(dirp->d_name, SIGNATURE_AUTHOR)) {
-            std::string fullPath = getFullPath(std::string(dirp->d_name));
+        if (!strcmp(result->d_name, SIGNATURE_AUTHOR)) {
+            std::string fullPath = getFullPath(std::string(result->d_name));
             LogDebug("Found author signature file full path : " << fullPath);
             set.insert(SignatureFileInfo(fullPath, -1));
             continue;
@@ -90,29 +94,29 @@ SignatureFinder::Result SignatureFinder::Impl::find(SignatureFileInfoSet &set)
         std::string sig;
         std::string num;
         std::string xml; /* just for cutting out .xml */
-        if (m_signatureRegexp.FullMatch(dirp->d_name, &sig, &num, &xml)) {
+        if (m_signatureRegexp.FullMatch(result->d_name, &sig, &num, &xml)) {
             std::istringstream stream(num);
             int number;
             stream >> number;
 
             if (stream.fail()) {
-                closedir(dp);
+                closedir(dirp);
                 return ERROR_ISTREAM;
             }
 
-            std::string fullPath = getFullPath(std::string(dirp->d_name));
+            std::string fullPath = getFullPath(std::string(result->d_name));
             LogDebug("Found signature file full path : " << fullPath);
             set.insert(SignatureFileInfo(fullPath, number));
         }
     }
 
-    if (errno != 0) {
+    if (ret != 0) {
         LogError("Error in readdir");
-        closedir(dp);
+        closedir(dirp);
         return ERROR_READING_DIR;
     }
 
-    closedir(dp);
+    closedir(dirp);
     return NO_ERROR;
 }