Skip to content

Commit

Permalink
Fix: stat was done on an incomplete file path. (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk authored Dec 16, 2021
1 parent d08d0e6 commit 169f05d
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions loader/linux/icd_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 169f05d

Please sign in to comment.