Fix: stat was done on an incomplete file path. (#161) upstream upstream/v2022.01.04
authorBrice Videau <bvideau@anl.gov>
Thu, 16 Dec 2021 06:58:26 +0000 (00:58 -0600)
committerGitHub <noreply@github.com>
Thu, 16 Dec 2021 06:58:26 +0000 (22:58 -0800)
loader/linux/icd_linux.c

index 7fa7bcd476267ff5462eff9f0c6cffb6dcf5f141..4c8da2a1b3771e6870242b5f436cd719719097e3 100644 (file)
@@ -60,37 +60,44 @@ void khrIcdOsVendorsEnumerate(void)
     else
     {
         // attempt to load all files in the directory
-        for (dirEntry = readdir(dir); dirEntry; dirEntry = readdir(dir) )
+        for (dirEntry = readdir(dir); dirEntry; dirEntry = readdir(dir))
         {
             struct stat statBuff;
-            stat(dirEntry->d_name, &statBuff);
+            const char* extension = ".icd";
+            char* fileName = NULL;
+
+            // make sure the file name ends in .icd
+            if (strlen(extension) > strlen(dirEntry->d_name))
+            {
+                continue;
+            }
+            if (strcmp(dirEntry->d_name + strlen(dirEntry->d_name) - strlen(extension), extension))
+            {
+                continue;
+            }
+
+            // allocate space for the full path of the vendor library name
+            fileName = malloc(strlen(dirEntry->d_name) + strlen(vendorPath) + 2);
+            if (!fileName)
+            {
+                KHR_ICD_TRACE("Failed allocate space for ICD file path\n");
+                continue;
+            }
+            sprintf(fileName, "%s/%s", vendorPath, dirEntry->d_name);
+
+            if (stat(fileName, &statBuff))
+            {
+                KHR_ICD_TRACE("Failed stat for: %s, continuing\n", fileName);
+                free(fileName);
+                continue;
+            }
+
             if (S_ISREG(statBuff.st_mode) || S_ISLNK(statBuff.st_mode))
             {
-                const char* extension = ".icd";
                 FILE *fin = NULL;
-                char* fileName = NULL;
                 char* buffer = NULL;
                 long bufferSize = 0;
 
-                // make sure the file name ends in .icd
-                if (strlen(extension) > strlen(dirEntry->d_name) )
-                {
-                    continue;
-                }
-                if (strcmp(dirEntry->d_name + strlen(dirEntry->d_name) - strlen(extension), extension) )
-                {
-                    continue;
-                }
-
-                // allocate space for the full path of the vendor library name
-                fileName = malloc(strlen(dirEntry->d_name) + strlen(vendorPath) + 2);
-                if (!fileName)
-                {
-                    KHR_ICD_TRACE("Failed allocate space for ICD file path\n");
-                    continue;
-                }
-                sprintf(fileName, "%s/%s", vendorPath, dirEntry->d_name);
-
                 // open the file and read its contents
                 fin = fopen(fileName, "r");
                 if (!fin)
@@ -110,7 +117,7 @@ void khrIcdOsVendorsEnumerate(void)
                 }
                 memset(buffer, 0, bufferSize+1);
                 fseek(fin, 0, SEEK_SET);
-                if (bufferSize != (long)fread(buffer, 1, bufferSize, fin) )
+                if (bufferSize != (long)fread(buffer, 1, bufferSize, fin))
                 {
                     free(fileName);
                     free(buffer);
@@ -129,6 +136,8 @@ void khrIcdOsVendorsEnumerate(void)
             }
             else
             {
+                KHR_ICD_TRACE("File %s is not a regular file nor symbolic link, continuing\n", fileName);
+                free(fileName);
                 continue;
             }
         }