diff --git a/plugins/libzipplugin/libzipplugin.cpp b/plugins/libzipplugin/libzipplugin.cpp index 0a8d153e07e97b4431b7d5bb6ccde0065b6c2705..8daa0fe7117fefabf40c415d84ce8d55deb15147 100644 --- a/plugins/libzipplugin/libzipplugin.cpp +++ b/plugins/libzipplugin/libzipplugin.cpp @@ -479,9 +479,9 @@ bool LibzipPlugin::testArchive() return false; } - zip_file *zipFile = zip_fopen_index(archive, i, 0); + std::unique_ptr zipFile { zip_fopen_index(archive, i, 0), &zip_fclose }; std::unique_ptr buf(new uchar[statBuffer.size]); - const int len = zip_fread(zipFile, buf.get(), statBuffer.size); + const int len = zip_fread(zipFile.get(), buf.get(), statBuffer.size); if (len == -1 || uint(len) != statBuffer.size) { qCCritical(ARK) << "Failed to read data for" << statBuffer.name; return false; @@ -673,11 +673,11 @@ bool LibzipPlugin::extractEntry(zip_t *archive, const QString &entry, const QStr } // Handle password-protected files. - zip_file *zipFile = nullptr; + std::unique_ptr zipFile { nullptr, &zip_fclose }; bool firstTry = true; - while (!zipFile) { - zipFile = zip_fopen(archive, entry.toUtf8().constData(), 0); - if (zipFile) { + while (!zipFile.get()) { + zipFile.reset(zip_fopen(archive, entry.toUtf8().constData(), 0)); + if (zipFile.get()) { break; } else if (zip_error_code_zip(zip_get_error(archive)) == ZIP_ER_NOPASSWD || zip_error_code_zip(zip_get_error(archive)) == ZIP_ER_WRONGPASSWD) { @@ -715,7 +715,7 @@ bool LibzipPlugin::extractEntry(zip_t *archive, const QString &entry, const QStr qulonglong sum = 0; char buf[1000]; while (sum != statBuffer.size) { - const auto readBytes = zip_fread(zipFile, buf, 1000); + const auto readBytes = zip_fread(zipFile.get(), buf, 1000); if (readBytes < 0) { qCCritical(ARK) << "Failed to read data"; emit error(xi18n("Failed to read data for entry: %1", entry));