From 90c8a1b1cf2433e934683ff75f5992ba467af4d7 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 9 Sep 2016 16:32:11 +0200 Subject: [PATCH] input/archive: use AtScopeExit() for exception-safety --- src/input/plugins/ArchiveInputPlugin.cxx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/input/plugins/ArchiveInputPlugin.cxx b/src/input/plugins/ArchiveInputPlugin.cxx index 8d83fa3f4..590155d0e 100644 --- a/src/input/plugins/ArchiveInputPlugin.cxx +++ b/src/input/plugins/ArchiveInputPlugin.cxx @@ -27,6 +27,7 @@ #include "../InputPlugin.hxx" #include "fs/Path.hxx" #include "Log.hxx" +#include "util/ScopeExit.hxx" #include @@ -34,15 +35,17 @@ InputStream * OpenArchiveInputStream(Path path, Mutex &mutex, Cond &cond, Error &error) { const ArchivePlugin *arplug; - InputStream *is; char *pname = strdup(path.c_str()); + AtScopeExit(pname) { + free(pname); + }; + // archive_lookup will modify pname when true is returned const char *archive, *filename, *suffix; if (!archive_lookup(pname, &archive, &filename, &suffix)) { FormatDebug(archive_domain, "not an archive, lookup %s failed", pname); - free(pname); return nullptr; } @@ -51,22 +54,19 @@ OpenArchiveInputStream(Path path, Mutex &mutex, Cond &cond, Error &error) if (!arplug) { FormatWarning(archive_domain, "can't handle archive %s", archive); - free(pname); return nullptr; } auto file = archive_file_open(arplug, Path::FromFS(archive), error); if (file == nullptr) { - free(pname); return nullptr; } - //setup fileops - is = file->OpenStream(filename, mutex, cond, error); - free(pname); - file->Close(); + AtScopeExit(file) { + file->Close(); + }; - return is; + return file->OpenStream(filename, mutex, cond, error); } static InputStream *