@@ -849,40 +849,67 @@ int SSL_CTX_load_verify_locations(SSL_CTX* ctx, const char* file,
849849 WIN32_FIND_DATA FindFileData;
850850 HANDLE hFind;
851851
852- char name[MAX_PATH + 1 ]; // directory specification
853- strncpy (name, path, MAX_PATH - 3 );
854- strncat (name, " \\ *" , 3 );
852+ const int DELIMITER_SZ = 2 ;
853+ const int DELIMITER_STAR_SZ = 3 ;
854+ int pathSz = (int )strlen (path);
855+ int nameSz = pathSz + DELIMITER_STAR_SZ + 1 ; // plus 1 for terminator
856+ char * name = NEW_YS char [nameSz]; // directory specification
857+ memset (name, 0 , nameSz);
858+ strncpy (name, path, nameSz - DELIMITER_STAR_SZ - 1 );
859+ strncat (name, " \\ *" , DELIMITER_STAR_SZ);
855860
856861 hFind = FindFirstFile (name, &FindFileData);
857- if (hFind == INVALID_HANDLE_VALUE) return SSL_BAD_PATH;
862+ if (hFind == INVALID_HANDLE_VALUE) {
863+ ysArrayDelete (name);
864+ return SSL_BAD_PATH;
865+ }
858866
859867 do {
860- if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
861- strncpy (name, path, MAX_PATH - 2 - HALF_PATH);
862- strncat (name, " \\ " , 2 );
863- strncat (name, FindFileData.cFileName , HALF_PATH);
868+ if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
869+ int curSz = (int )strlen (FindFileData.cFileName );
870+ if (pathSz + curSz + DELIMITER_SZ + 1 > nameSz) {
871+ ysArrayDelete (name);
872+ // plus 1 for terminator
873+ nameSz = pathSz + curSz + DELIMITER_SZ + 1 ;
874+ name = NEW_YS char [nameSz];
875+ }
876+ memset (name, 0 , nameSz);
877+ strncpy (name, path, nameSz - curSz - DELIMITER_SZ - 1 );
878+ strncat (name, " \\ " , DELIMITER_SZ);
879+ strncat (name, FindFileData.cFileName ,
880+ nameSz - pathSz - DELIMITER_SZ - 1 );
864881 ret = read_file (ctx, name, SSL_FILETYPE_PEM, CA);
865882 }
866883 } while (ret == SSL_SUCCESS && FindNextFile (hFind, &FindFileData));
867884
885+ ysArrayDelete (name);
868886 FindClose (hFind);
869887
870888#else // _WIN32
871-
872- const int MAX_PATH = 260 ;
873-
874889 DIR* dir = opendir (path);
875890 if (!dir) return SSL_BAD_PATH;
876891
877892 struct dirent * entry;
878893 struct stat buf;
879- char name[MAX_PATH + 1 ];
894+ const int DELIMITER_SZ = 1 ;
895+ int pathSz = (int )strlen (path);
896+ int nameSz = pathSz + DELIMITER_SZ + 1 ; // plus 1 for null terminator
897+ char * name = NEW_YS char [nameSz]; // directory specification
880898
881899 while (ret == SSL_SUCCESS && (entry = readdir (dir))) {
882- strncpy (name, path, MAX_PATH - 1 - HALF_PATH);
883- strncat (name, " /" , 1 );
884- strncat (name, entry->d_name , HALF_PATH);
900+ int curSz = (int )strlen (entry->d_name );
901+ if (pathSz + curSz + DELIMITER_SZ + 1 > nameSz) {
902+ ysArrayDelete (name);
903+ nameSz = pathSz + DELIMITER_SZ + curSz + 1 ;
904+ name = NEW_YS char [nameSz];
905+ }
906+ memset (name, 0 , nameSz);
907+ strncpy (name, path, nameSz - curSz - 1 );
908+ strncat (name, " /" , DELIMITER_SZ);
909+ strncat (name, entry->d_name , nameSz - pathSz - DELIMITER_SZ - 1 );
910+
885911 if (stat (name, &buf) < 0 ) {
912+ ysArrayDelete (name);
886913 closedir (dir);
887914 return SSL_BAD_STAT;
888915 }
@@ -891,6 +918,7 @@ int SSL_CTX_load_verify_locations(SSL_CTX* ctx, const char* file,
891918 ret = read_file (ctx, name, SSL_FILETYPE_PEM, CA);
892919 }
893920
921+ ysArrayDelete (name);
894922 closedir (dir);
895923
896924#endif
0 commit comments