diff options
author | Brice Videau <bvideau@anl.gov> | 2021-12-16 00:58:26 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-15 22:58:26 -0800 |
commit | 169f05d026e65948b30cfe2200595fda92198cf7 (patch) | |
tree | 7dfca247a9a30dcc8084f1d0e55891368f43e532 | |
parent | d08d0e6452c15502c6eb7330eebb2f1264b958e8 (diff) | |
download | OpenCL-ICD-Loader-169f05d026e65948b30cfe2200595fda92198cf7.tar.gz OpenCL-ICD-Loader-169f05d026e65948b30cfe2200595fda92198cf7.tar.bz2 OpenCL-ICD-Loader-169f05d026e65948b30cfe2200595fda92198cf7.zip |
Fix: stat was done on an incomplete file path. (#161)upstream/v2022.01.04upstream
-rw-r--r-- | loader/linux/icd_linux.c | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/loader/linux/icd_linux.c b/loader/linux/icd_linux.c index 7fa7bcd..4c8da2a 100644 --- a/loader/linux/icd_linux.c +++ b/loader/linux/icd_linux.c @@ -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; } } |