From 96c137c1a2f2f7f0f9dd5f54ab92b117e97e2324 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 30 Jun 2022 04:41:50 -0700 Subject: [PATCH] Core: Fix leaks and clean up mDirectorySet --- src/core/directories.c | 159 ++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 107 deletions(-) diff --git a/src/core/directories.c b/src/core/directories.c index e2e2d2c9f..63c66e564 100644 --- a/src/core/directories.c +++ b/src/core/directories.c @@ -10,92 +10,53 @@ #if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2 void mDirectorySetInit(struct mDirectorySet* dirs) { - dirs->base = 0; - dirs->archive = 0; - dirs->save = 0; - dirs->patch = 0; - dirs->state = 0; - dirs->screenshot = 0; - dirs->cheats = 0; + dirs->base = NULL; + dirs->archive = NULL; + dirs->save = NULL; + dirs->patch = NULL; + dirs->state = NULL; + dirs->screenshot = NULL; + dirs->cheats = NULL; +} + +static void mDirectorySetDetachDir(struct mDirectorySet* dirs, struct VDir* dir) { + if (!dir) { + return; + } + + if (dirs->base == dir) { + dirs->base = NULL; + } + if (dirs->archive == dir) { + dirs->archive = NULL; + } + if (dirs->save == dir) { + dirs->save = NULL; + } + if (dirs->patch == dir) { + dirs->patch = NULL; + } + if (dirs->state == dir) { + dirs->state = NULL; + } + if (dirs->screenshot == dir) { + dirs->screenshot = NULL; + } + if (dirs->cheats == dir) { + dirs->cheats = NULL; + } + + dir->close(dir); } void mDirectorySetDeinit(struct mDirectorySet* dirs) { mDirectorySetDetachBase(dirs); - - if (dirs->archive) { - if (dirs->archive == dirs->save) { - dirs->save = NULL; - } - if (dirs->archive == dirs->patch) { - dirs->patch = NULL; - } - if (dirs->archive == dirs->state) { - dirs->state = NULL; - } - if (dirs->archive == dirs->screenshot) { - dirs->screenshot = NULL; - } - if (dirs->archive == dirs->cheats) { - dirs->cheats = NULL; - } - dirs->archive->close(dirs->archive); - dirs->archive = NULL; - } - - if (dirs->save) { - if (dirs->save == dirs->patch) { - dirs->patch = NULL; - } - if (dirs->save == dirs->state) { - dirs->state = NULL; - } - if (dirs->save == dirs->screenshot) { - dirs->screenshot = NULL; - } - if (dirs->save == dirs->cheats) { - dirs->cheats = NULL; - } - dirs->save->close(dirs->save); - dirs->save = NULL; - } - - if (dirs->patch) { - if (dirs->patch == dirs->state) { - dirs->state = NULL; - } - if (dirs->patch == dirs->screenshot) { - dirs->screenshot = NULL; - } - if (dirs->patch == dirs->cheats) { - dirs->cheats = NULL; - } - dirs->patch->close(dirs->patch); - dirs->patch = NULL; - } - - if (dirs->state) { - if (dirs->state == dirs->screenshot) { - dirs->state = NULL; - } - if (dirs->state == dirs->cheats) { - dirs->cheats = NULL; - } - dirs->state->close(dirs->state); - dirs->state = NULL; - } - - if (dirs->screenshot) { - if (dirs->screenshot == dirs->cheats) { - dirs->cheats = NULL; - } - dirs->screenshot->close(dirs->screenshot); - dirs->screenshot = NULL; - } - - if (dirs->cheats) { - dirs->cheats->close(dirs->cheats); - dirs->cheats = NULL; - } + mDirectorySetDetachDir(dirs, dirs->archive); + mDirectorySetDetachDir(dirs, dirs->save); + mDirectorySetDetachDir(dirs, dirs->patch); + mDirectorySetDetachDir(dirs, dirs->state); + mDirectorySetDetachDir(dirs, dirs->screenshot); + mDirectorySetDetachDir(dirs, dirs->cheats); } void mDirectorySetAttachBase(struct mDirectorySet* dirs, struct VDir* base) { @@ -118,36 +79,20 @@ void mDirectorySetAttachBase(struct mDirectorySet* dirs, struct VDir* base) { } void mDirectorySetDetachBase(struct mDirectorySet* dirs) { - if (dirs->save == dirs->base) { - dirs->save = NULL; - } - if (dirs->patch == dirs->base) { - dirs->patch = NULL; - } - if (dirs->state == dirs->base) { - dirs->state = NULL; - } - if (dirs->screenshot == dirs->base) { - dirs->screenshot = NULL; - } - if (dirs->cheats == dirs->base) { - dirs->cheats = NULL; - } - - if (dirs->base) { - dirs->base->close(dirs->base); - dirs->base = NULL; - } + mDirectorySetDetachDir(dirs, dirs->archive); + mDirectorySetDetachDir(dirs, dirs->base); } struct VFile* mDirectorySetOpenPath(struct mDirectorySet* dirs, const char* path, bool (*filter)(struct VFile*)) { - dirs->archive = VDirOpenArchive(path); + struct VDir* archive = VDirOpenArchive(path); struct VFile* file; - if (dirs->archive) { - file = VDirFindFirst(dirs->archive, filter); + if (archive) { + file = VDirFindFirst(archive, filter); if (!file) { - dirs->archive->close(dirs->archive); - dirs->archive = 0; + archive->close(archive); + } else { + mDirectorySetDetachDir(dirs, dirs->archive); + dirs->archive = archive; } } else { file = VFileOpen(path, O_RDONLY);