diff --git a/src/Utilities/Compression/QGCZlib.cc b/src/Utilities/Compression/QGCZlib.cc index 9b52b304aac..44e8dde2f67 100644 --- a/src/Utilities/Compression/QGCZlib.cc +++ b/src/Utilities/Compression/QGCZlib.cc @@ -16,7 +16,8 @@ QGC_LOGGING_CATEGORY(QGCZlibLog, "qgc.compression.qgczlib") -namespace QGCZlib { +namespace QGCZlib +{ bool inflateGzipFile(const QString &gzippedFileName, const QString &decompressedFilename) { @@ -35,7 +36,8 @@ bool inflateGzipFile(const QString &gzippedFileName, const QString &decompressed QFile outputFile(decompressedFilename); if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) { - qCWarning(QGCZlibLog) << "open input file failed" << outputFile.fileName() << outputFile.errorString(); + qCWarning(QGCZlibLog) << "open output file failed" << outputFile.fileName() << outputFile.errorString(); + inputFile.close(); return false; } @@ -48,7 +50,9 @@ bool inflateGzipFile(const QString &gzippedFileName, const QString &decompressed ret = inflateInit2(&strm, 16 + MAX_WBITS); if (ret != Z_OK) { qCWarning(QGCZlibLog) << "inflateInit2 failed:" << ret; - goto Error; + inputFile.close(); + outputFile.close(); + return false; } do { @@ -63,28 +67,36 @@ bool inflateGzipFile(const QString &gzippedFileName, const QString &decompressed strm.next_out = outputBuffer; ret = inflate(&strm, Z_NO_FLUSH); - if ((ret != Z_OK) && (ret != Z_STREAM_END)) { + if (ret == Z_STREAM_ERROR || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) { qCWarning(QGCZlibLog) << "inflate failed:" << ret; - goto Error; + inflateEnd(&strm); + inputFile.close(); + outputFile.close(); + return false; } const unsigned cBytesInflated = cBuffer - strm.avail_out; - const qint64 cBytesWritten = outputFile.write(reinterpret_cast(outputBuffer), static_cast(cBytesInflated)); - if (cBytesWritten != cBytesInflated) { + if (outputFile.write(reinterpret_cast(outputBuffer), cBytesInflated) != cBytesInflated) { qCWarning(QGCZlibLog) << "output file write failed:" << outputFile.fileName() << outputFile.errorString(); - goto Error; - + inflateEnd(&strm); + inputFile.close(); + outputFile.close(); + return false; } } while (strm.avail_out == 0); + } while (ret != Z_STREAM_END); -Out: inflateEnd(&strm); - return success; + inputFile.close(); + outputFile.close(); -Error: - success = false; - goto Out; + if (ret != Z_STREAM_END) { + qCWarning(QGCZlibLog) << "inflate did not reach stream end:" << ret; + return false; + } + + return success; } } // namespace QGCZlib diff --git a/src/Utilities/Compression/QGCZlib.h b/src/Utilities/Compression/QGCZlib.h index 81f19f7dc71..6c1c6c2eeec 100644 --- a/src/Utilities/Compression/QGCZlib.h +++ b/src/Utilities/Compression/QGCZlib.h @@ -14,9 +14,11 @@ Q_DECLARE_LOGGING_CATEGORY(QGCZlibLog) -namespace QGCZlib { +namespace QGCZlib +{ /// Decompresses the specified file to the specified directory /// @param gzippedFileName Fully qualified path to gzip file /// @param decompressedFilename Fully qualified path to for file to decompress to + /// @return bool Success bool inflateGzipFile(const QString &gzippedFileName, const QString &decompressedFilename); -} // namespace QGCZlib +}