Fix some memory leaks

This commit is contained in:
Maschell 2021-01-01 01:52:20 +01:00
parent c9a655eeee
commit aec8fc5035

View File

@ -76,13 +76,14 @@ void VirtualMountDevice(const char * path)
void AddVirtualPath(const char *name, const char *alias, const char *prefix) void AddVirtualPath(const char *name, const char *alias, const char *prefix)
{ {
if(!VIRTUAL_PARTITIONS) if (!VIRTUAL_PARTITIONS) {
VIRTUAL_PARTITIONS = (VIRTUAL_PARTITION *) malloc(sizeof(VIRTUAL_PARTITION)); VIRTUAL_PARTITIONS = (VIRTUAL_PARTITION *) malloc(sizeof(VIRTUAL_PARTITION));
}
VIRTUAL_PARTITION *tmp = realloc(VIRTUAL_PARTITIONS, sizeof(VIRTUAL_PARTITION) * (MAX_VIRTUAL_PARTITIONS + 1)); VIRTUAL_PARTITION *tmp = realloc(VIRTUAL_PARTITIONS, sizeof(VIRTUAL_PARTITION) * (MAX_VIRTUAL_PARTITIONS + 1));
if(!tmp) if (!tmp) {
{
free(VIRTUAL_PARTITIONS); free(VIRTUAL_PARTITIONS);
VIRTUAL_PARTITIONS = NULL;
MAX_VIRTUAL_PARTITIONS = 0; MAX_VIRTUAL_PARTITIONS = 0;
return; return;
} }
@ -105,7 +106,10 @@ void AddVirtualPath(const char *name, const char *alias, const char *prefix)
VIRTUAL_PARTITION *tmp = realloc(VIRTUAL_FS, sizeof(VIRTUAL_PARTITION) * (MAX_VIRTUAL_FS + 1)); VIRTUAL_PARTITION *tmp = realloc(VIRTUAL_FS, sizeof(VIRTUAL_PARTITION) * (MAX_VIRTUAL_FS + 1));
if (!tmp) { if (!tmp) {
if (VIRTUAL_FS) {
free(VIRTUAL_FS); free(VIRTUAL_FS);
VIRTUAL_FS = 0;
}
MAX_VIRTUAL_FS = 0; MAX_VIRTUAL_FS = 0;
return; return;
} }
@ -150,21 +154,45 @@ void MountVirtualDevices()
VirtualMountDevice("usb:/"); VirtualMountDevice("usb:/");
} }
void UnmountVirtualPaths() void UnmountVirtualPaths() {
{
uint32_t i = 0; uint32_t i = 0;
for(i = 0; i < MAX_VIRTUAL_PARTITIONS; i++) for (i = 0; i < MAX_VIRTUAL_PARTITIONS; i++) {
{ if (VIRTUAL_PARTITIONS[i].name) {
if(VIRTUAL_PARTITIONS[i].name)
free(VIRTUAL_PARTITIONS[i].name); free(VIRTUAL_PARTITIONS[i].name);
if(VIRTUAL_PARTITIONS[i].alias) }
if (VIRTUAL_PARTITIONS[i].alias) {
free(VIRTUAL_PARTITIONS[i].alias); free(VIRTUAL_PARTITIONS[i].alias);
if(VIRTUAL_PARTITIONS[i].prefix) }
if (VIRTUAL_PARTITIONS[i].prefix) {
free(VIRTUAL_PARTITIONS[i].prefix); free(VIRTUAL_PARTITIONS[i].prefix);
} }
}
if(VIRTUAL_PARTITIONS)
free(VIRTUAL_PARTITIONS); for (i = 0; i < MAX_VIRTUAL_FS_VOL; i++) {
VIRTUAL_PARTITIONS = NULL; if (VIRTUAL_FS_VOL[i].name) {
MAX_VIRTUAL_PARTITIONS = 0; free(VIRTUAL_FS_VOL[i].name);
}
}
for (i = 0; i < MAX_VIRTUAL_FS; i++) {
if (VIRTUAL_FS[i].name) {
free(VIRTUAL_FS[i].name);
}
}
if (VIRTUAL_PARTITIONS) {
free(VIRTUAL_PARTITIONS);
}
if (VIRTUAL_FS_VOL) {
free(VIRTUAL_FS_VOL);
}
if (VIRTUAL_FS) {
free(VIRTUAL_FS);
}
VIRTUAL_PARTITIONS = NULL;
VIRTUAL_FS_VOL = NULL;
VIRTUAL_FS = NULL;
MAX_VIRTUAL_PARTITIONS = 0;
MAX_VIRTUAL_FS = 0;
MAX_VIRTUAL_FS_VOL = 0;
} }