Skip to content

Commit fbacfca

Browse files
committed
review 2 -- just use NAME_MAX
1 parent ad6a438 commit fbacfca

File tree

1 file changed

+7
-12
lines changed
  • src/native/libs/System.Security.Cryptography.Native

1 file changed

+7
-12
lines changed

src/native/libs/System.Security.Cryptography.Native/pal_x509.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,6 @@ X509* CryptoNative_X509UpRef(X509* x509)
435435
return x509;
436436
}
437437

438-
// On some platforms (like illumos), d_name is declared as char[1], so sizeof(d_name) doesn't give
439-
// the actual maximum length. Use NAME_MAX or MAXNAMLEN instead.
440-
#ifdef NAME_MAX
441-
static size_t maxNameLen = NAME_MAX;
442-
#else
443-
static size_t maxNameLen = 255; // Reasonable default
444-
#endif
445-
446438
static DIR* OpenUserStore(const char* storePath, char** pathTmp, size_t* pathTmpSize, char** nextFileWrite)
447439
{
448440
DIR* trustDir = opendir(storePath);
@@ -456,9 +448,12 @@ static DIR* OpenUserStore(const char* storePath, char** pathTmp, size_t* pathTmp
456448

457449
size_t storePathLen = strlen(storePath);
458450

459-
// Allocate enough space for the store path, a '/', the maximum filename length, and a null terminator
460-
461-
size_t allocSize = storePathLen + maxNameLen + 2; // +2 for '/' and '\0'
451+
// d_name is a fixed length char[], not a char*.
452+
// The traditional declaration (eg. SunOS) is d_name[1]
453+
// so we can't assume sizeof(ent->d_name) will work,
454+
// so just use NAME_MAX which is the POSIX way.
455+
// Leave one byte for '\0' and one for '/'
456+
size_t allocSize = storePathLen + NAME_MAX + 2;
462457
char* tmp = (char*)calloc(allocSize, sizeof(char));
463458
if (!tmp)
464459
{
@@ -488,7 +483,7 @@ static X509* ReadNextPublicCert(DIR* dir, X509Stack* tmpStack, char* pathTmp, si
488483

489484
while ((next = readdir(dir)) != NULL)
490485
{
491-
size_t len = strnlen(next->d_name, maxNameLen);
486+
size_t len = strnlen(next->d_name, NAME_MAX);
492487

493488
if (len > 4 && 0 == strncasecmp(".pfx", next->d_name + len - 4, 4))
494489
{

0 commit comments

Comments
 (0)