fix: improved memory safety in getVaultsFromDirectories()

This commit is contained in:
2026-04-26 21:14:17 +02:00
parent 3233b330db
commit 5c87ac0aa2
+7 -4
View File
@@ -127,14 +127,17 @@ char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber
altDebug("%s", entryName); altDebug("%s", entryName);
if (entryName[0] != '.') { // if not hidden file/dir if (entryName[0] != '.') { // if not hidden file/dir
char tempFullEntryPath[PATH_MAX]; // we recreate the full path to check it's proprieties char tempFullEntryPath[PATH_MAX]; // we recreate the full path to check it's proprieties
snprintf(tempFullEntryPath, PATH_MAX, "%s/%s", directoryStringArray[i], entryName); snprintf(tempFullEntryPath, PATH_MAX, "%s%s", directoryStringArray[i], entryName);
altDebug(" (%s)", tempFullEntryPath);
//checking the metadata to see if it is a dir //checking the metadata to see if it is a dir
struct stat metadataEntry; struct stat metadataEntry;
if (stat(tempFullEntryPath, &metadataEntry) == 0 && S_ISDIR(metadataEntry.st_mode)) { // get's the metadata (stat should return 0 if fails) and sees if it is a dir. if (stat(tempFullEntryPath, &metadataEntry) == 0 && S_ISDIR(metadataEntry.st_mode)) { // get's the metadata (stat should return 0 if fails) and sees if it is a dir.
altDebug(" is a vault\n"); altDebug(" is a vault\n");
vaultsArray = realloc(vaultsArray, + sizeof(char *)*(nthVault + 1)); // resize vaultsArray vaultsArray = realloc(vaultsArray, sizeof(char *)*(nthVault + 1)); // resize vaultsArray
error(vaultsArray == NULL, "program", "realloc failed");
vaultsPerDirectoryNumber[i]++; // it will be used later to know which vaults goes into which directory vaultsPerDirectoryNumber[i]++; // it will be used later to know which vaults goes into which directory
vaultsArray[nthVault] = strdup(entryName); // we use strdup and not strcpy, because memory used with opendir and readdir will be closed. vaultsArray[nthVault] = strdup(entryName); // we use strdup and not strcpy, because memory used with opendir and readdir will be closed.
error(vaultsArray[nthVault] == NULL, "program", "strdup failed");
nthVault++; // it is used immediatly to set the vault into directoryStringArray nthVault++; // it is used immediatly to set the vault into directoryStringArray
} else { } else {
altDebug(" is not a vault (not a dir.)\n"); altDebug(" is not a vault (not a dir.)\n");
@@ -146,9 +149,9 @@ char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber
closedir(vaultsDirectory); closedir(vaultsDirectory);
} }
// we calculate once the total number of vaults to avoid recalculation every time we use it // we calculate once the total number of vaults to avoid recalculation every time we use it
count = 0; *count = 0;
for (int i = 0; i < directoryNumber; i++) { for (int i = 0; i < directoryNumber; i++) {
count += vaultsPerDirectoryNumber[i]; *count += vaultsPerDirectoryNumber[i];
} }
return vaultsArray; return vaultsArray;
} }