Skip to content

Commit

Permalink
free allocated memory after use
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan200101 committed Jul 28, 2024
1 parent c41b3fd commit 8e531a1
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions sgdboop.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,9 @@ char* getMostRecentUser(char* steamBaseDir) {
size_t len = 0;
size_t read;
fp = fopen(steamConfigFile, "r");
free(steamConfigFile);
if (fp == NULL) {
free(steamid);
free(steamConfigFile);
exitWithError("Couldn't find logged in user", 95);
}

Expand Down Expand Up @@ -470,6 +470,7 @@ char* getSteamDestinationDir(char* type, struct nonSteamApp* nonSteamAppData) {
strcat(steamBaseDir, "/userdata/");
strcat(steamBaseDir, steamid);
strcat(steamBaseDir, "/config/grid/");
free(steamid);
}

return steamBaseDir;
Expand Down Expand Up @@ -544,10 +545,13 @@ struct nonSteamApp* getSourceMods(const char* type)
if (fp_reg == NULL) {
char errorMessage[500];
sprintf(errorMessage, "File registry.vdf could not be found in %s", regFileLocation);
free(regFileLocation);
logError(errorMessage, 96);
return NULL;
}

free(regFileLocation);

while ((read_reg = readLine(&line_reg, &len_reg, fp_reg)) != -1) {

// If line contains the regvalue's key, capture the value
Expand All @@ -565,6 +569,7 @@ struct nonSteamApp* getSourceMods(const char* type)
break;
}
}
free(line_reg);

// Replace "//" with "\"
if (foundValue) {
Expand All @@ -587,6 +592,7 @@ struct nonSteamApp* getSourceMods(const char* type)
{
char errorMessage[500];
sprintf(errorMessage, "Could not read directory %s", sourceModPath);
free(sourceModPath);
logError(errorMessage, 98);
return NULL;
}
Expand Down Expand Up @@ -706,6 +712,9 @@ struct nonSteamApp* getSourceMods(const char* type)
free(line);
}
closedir(dr);
free(filepath);

free(sourceModPath);

if (goldsource) {
_goldSourceModsCount = modsCount;
Expand Down Expand Up @@ -738,8 +747,15 @@ struct nonSteamApp* getSourceMods(const char* type)
sprintf(sourceMods[i].appid, "%lu", 2147483649 + hex_index);
strcpy(sourceMods[i].name, sourceModsNames[i]);
sprintf(sourceMods[i].appid_old, "%" PRIu64, appid_old);

free(sourceModsNames[i]);
free(sourceModsDirs[i]);
}

free(sourceModsNames);
free(sourceModsSteamAppIds);
free(sourceModsDirs);

return sourceMods;
}

Expand All @@ -756,18 +772,22 @@ struct nonSteamApp* getNonSteamApps(int includeMods) {
strcat(shortcutsVdfPath, steamid);
strcat(shortcutsVdfPath, "/config/shortcuts.vdf");

free(steamid);

// Parse the file
FILE* fp;
unsigned char buf[2] = { 0 };
size_t bytes = 0;
size_t read = sizeof buf;
fp = fopen(shortcutsVdfPath, "rb");
free(shortcutsVdfPath);
if (fp != NULL) {
fseek(fp, 0L, SEEK_END);
size_t filesize = ftell(fp) + 2;
fseek(fp, 0, SEEK_SET);

unsigned char* fileContent = malloc(filesize + 1);
memset(fileContent, '\xAB', filesize);
unsigned char* realFileContent = malloc(filesize + 1);
unsigned int currentFileByte = 0;

Expand Down Expand Up @@ -857,6 +877,9 @@ struct nonSteamApp* getNonSteamApps(int includeMods) {
*nameEndChar = 0x03; // Revert name string to prevent string-related problems
parsingChar = appBlockEndPtr + 2;
}

free(fileContent);
free(realFileContent);
}

// Add source (and goldsource) mods
Expand All @@ -881,6 +904,8 @@ struct nonSteamApp* getNonSteamApps(int includeMods) {

_nonSteamAppsCount++;
}
free(sourceMods);
free(goldSourceMods);
}


Expand Down Expand Up @@ -978,14 +1003,16 @@ void updateVdf(struct nonSteamApp* appData, char* filePath) {
strcat(shortcutsVdfPath, steamid);
strcat(shortcutsVdfPath, "/config/shortcuts.vdf");

free(steamid);

// Parse the file
FILE* fp;
unsigned char buf[1] = { 0 };
size_t bytes = 0;
size_t read = sizeof buf;
fp = fopen(shortcutsVdfPath, "rb");
free(shortcutsVdfPath);
if (fp == NULL) {
free(shortcutsVdfPath);
exitWithError("Shortcuts vdf could not be found.", 93);
}
fseek(fp, 0L, SEEK_END);
Expand Down Expand Up @@ -1221,6 +1248,8 @@ int main(int argc, char** argv)
}

}

free(steamDestDir);
}

free(nonSteamAppData);
Expand Down

0 comments on commit 8e531a1

Please sign in to comment.