Skip to content

Commit 169f05d

Browse files
authored
Fix: stat was done on an incomplete file path. (#161)
1 parent d08d0e6 commit 169f05d

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

loader/linux/icd_linux.c

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,37 +60,44 @@ void khrIcdOsVendorsEnumerate(void)
6060
else
6161
{
6262
// attempt to load all files in the directory
63-
for (dirEntry = readdir(dir); dirEntry; dirEntry = readdir(dir) )
63+
for (dirEntry = readdir(dir); dirEntry; dirEntry = readdir(dir))
6464
{
6565
struct stat statBuff;
66-
stat(dirEntry->d_name, &statBuff);
66+
const char* extension = ".icd";
67+
char* fileName = NULL;
68+
69+
// make sure the file name ends in .icd
70+
if (strlen(extension) > strlen(dirEntry->d_name))
71+
{
72+
continue;
73+
}
74+
if (strcmp(dirEntry->d_name + strlen(dirEntry->d_name) - strlen(extension), extension))
75+
{
76+
continue;
77+
}
78+
79+
// allocate space for the full path of the vendor library name
80+
fileName = malloc(strlen(dirEntry->d_name) + strlen(vendorPath) + 2);
81+
if (!fileName)
82+
{
83+
KHR_ICD_TRACE("Failed allocate space for ICD file path\n");
84+
continue;
85+
}
86+
sprintf(fileName, "%s/%s", vendorPath, dirEntry->d_name);
87+
88+
if (stat(fileName, &statBuff))
89+
{
90+
KHR_ICD_TRACE("Failed stat for: %s, continuing\n", fileName);
91+
free(fileName);
92+
continue;
93+
}
94+
6795
if (S_ISREG(statBuff.st_mode) || S_ISLNK(statBuff.st_mode))
6896
{
69-
const char* extension = ".icd";
7097
FILE *fin = NULL;
71-
char* fileName = NULL;
7298
char* buffer = NULL;
7399
long bufferSize = 0;
74100

75-
// make sure the file name ends in .icd
76-
if (strlen(extension) > strlen(dirEntry->d_name) )
77-
{
78-
continue;
79-
}
80-
if (strcmp(dirEntry->d_name + strlen(dirEntry->d_name) - strlen(extension), extension) )
81-
{
82-
continue;
83-
}
84-
85-
// allocate space for the full path of the vendor library name
86-
fileName = malloc(strlen(dirEntry->d_name) + strlen(vendorPath) + 2);
87-
if (!fileName)
88-
{
89-
KHR_ICD_TRACE("Failed allocate space for ICD file path\n");
90-
continue;
91-
}
92-
sprintf(fileName, "%s/%s", vendorPath, dirEntry->d_name);
93-
94101
// open the file and read its contents
95102
fin = fopen(fileName, "r");
96103
if (!fin)
@@ -110,7 +117,7 @@ void khrIcdOsVendorsEnumerate(void)
110117
}
111118
memset(buffer, 0, bufferSize+1);
112119
fseek(fin, 0, SEEK_SET);
113-
if (bufferSize != (long)fread(buffer, 1, bufferSize, fin) )
120+
if (bufferSize != (long)fread(buffer, 1, bufferSize, fin))
114121
{
115122
free(fileName);
116123
free(buffer);
@@ -129,6 +136,8 @@ void khrIcdOsVendorsEnumerate(void)
129136
}
130137
else
131138
{
139+
KHR_ICD_TRACE("File %s is not a regular file nor symbolic link, continuing\n", fileName);
140+
free(fileName);
132141
continue;
133142
}
134143
}

0 commit comments

Comments
 (0)