improved Zip error handling (getting rid of poco_assert)

This commit is contained in:
Günter Obiltschnig
2016-09-29 19:13:21 +02:00
parent 4a96c934ba
commit 0e0fac80b6
7 changed files with 32 additions and 13 deletions

View File

@@ -80,7 +80,7 @@ void Compress::addEntry(std::istream& in, const Poco::DateTime& lastModifiedAt,
if (hdr.searchCRCAndSizesAfterData())
_offset += ZipDataInfo::getFullHeaderSize();
_files.insert(std::make_pair(fileName.toString(Poco::Path::PATH_UNIX), hdr));
poco_assert (_out);
if (!_out) throw Poco::IOException("Bad output stream");
ZipFileInfo nfo(hdr);
nfo.setOffset(localHeaderOffset);
_infos.insert(std::make_pair(fileName.toString(Poco::Path::PATH_UNIX), nfo));
@@ -145,7 +145,7 @@ void Compress::addFileRaw(std::istream& in, const ZipLocalFileHeader& h, const P
if (hdr.searchCRCAndSizesAfterData())
_offset += ZipDataInfo::getFullHeaderSize();
_files.insert(std::make_pair(fileName.toString(Poco::Path::PATH_UNIX), hdr));
poco_assert (_out);
if (!_out) throw Poco::IOException("Bad output stream");
ZipFileInfo nfo(hdr);
nfo.setOffset(localHeaderOffset);
_infos.insert(std::make_pair(fileName.toString(Poco::Path::PATH_UNIX), nfo));
@@ -213,7 +213,7 @@ void Compress::addDirectory(const Poco::Path& entryName, const Poco::DateTime& l
if (hdr.searchCRCAndSizesAfterData())
_offset += ZipDataInfo::getFullHeaderSize();
_files.insert(std::make_pair(entryName.toString(Poco::Path::PATH_UNIX), hdr));
poco_assert (_out);
if (!_out) throw Poco::IOException("Bad output stream");
ZipFileInfo nfo(hdr);
nfo.setOffset(localHeaderOffset);
_infos.insert(std::make_pair(entryName.toString(Poco::Path::PATH_UNIX), nfo));
@@ -294,8 +294,7 @@ ZipArchive Compress::close()
centralDirSize += entrySize;
_offset += entrySize;
}
poco_assert (_out);
if (!_out) throw Poco::IOException("Bad output stream");
Poco::UInt16 numEntries = static_cast<Poco::UInt16>(_infos.size());
ZipArchiveInfo central;

View File

@@ -39,7 +39,7 @@ Decompress::Decompress(std::istream& in, const Poco::Path& outputDir, bool flatt
{
_outDir.makeAbsolute();
_outDir.makeDirectory();
poco_assert (_in.good());
if (!_in.good()) throw Poco::IOException("Bad input stream");
Poco::File tmp(_outDir);
if (!tmp.exists())
{

View File

@@ -58,12 +58,16 @@ void ZipArchiveInfo::parse(std::istream& inp, bool assumeHeaderRead)
if (!assumeHeaderRead)
{
inp.read(_rawInfo, ZipCommon::HEADER_SIZE);
if (inp.gcount() != ZipCommon::HEADER_SIZE)
throw Poco::IOException("Failed to read archive info header");
if (std::memcmp(_rawInfo, HEADER, ZipCommon::HEADER_SIZE) != 0)
throw Poco::DataFormatException("Bad archive info header");
}
else
{
std::memcpy(_rawInfo, HEADER, ZipCommon::HEADER_SIZE);
}
poco_assert (std::memcmp(_rawInfo, HEADER, ZipCommon::HEADER_SIZE) == 0);
// read the rest of the header
inp.read(_rawInfo + ZipCommon::HEADER_SIZE, FULLHEADER_SIZE - ZipCommon::HEADER_SIZE);
Poco::UInt16 len = getZipCommentSize();

View File

@@ -15,6 +15,7 @@
#include "Poco/Zip/ZipDataInfo.h"
#include "Poco/Exception.h"
#include <istream>
#include <cstring>
@@ -41,10 +42,17 @@ ZipDataInfo::ZipDataInfo(std::istream& in, bool assumeHeaderRead):
_valid(false)
{
if (assumeHeaderRead)
{
std::memcpy(_rawInfo, HEADER, ZipCommon::HEADER_SIZE);
}
else
{
in.read(_rawInfo, ZipCommon::HEADER_SIZE);
poco_assert (std::memcmp(_rawInfo, HEADER, ZipCommon::HEADER_SIZE) == 0);
if (in.gcount() != ZipCommon::HEADER_SIZE)
throw Poco::IOException("Failed to read data info header");
if (std::memcmp(_rawInfo, HEADER, ZipCommon::HEADER_SIZE) != 0)
throw Poco::DataFormatException("Bad data info header");
}
// now copy the rest of the header
in.read(_rawInfo+ZipCommon::HEADER_SIZE, FULLHEADER_SIZE - ZipCommon::HEADER_SIZE);
_valid = (!in.eof() && in.good());

View File

@@ -85,12 +85,16 @@ void ZipFileInfo::parse(std::istream& inp, bool assumeHeaderRead)
if (!assumeHeaderRead)
{
inp.read(_rawInfo, ZipCommon::HEADER_SIZE);
if (inp.gcount() != ZipCommon::HEADER_SIZE)
throw Poco::IOException("Failed to read file info header");
if (std::memcmp(_rawInfo, HEADER, ZipCommon::HEADER_SIZE) != 0)
throw Poco::DataFormatException("Bad file info header");
}
else
{
std::memcpy(_rawInfo, HEADER, ZipCommon::HEADER_SIZE);
}
poco_assert (std::memcmp(_rawInfo, HEADER, ZipCommon::HEADER_SIZE) == 0);
// read the rest of the header
inp.read(_rawInfo + ZipCommon::HEADER_SIZE, FULLHEADER_SIZE - ZipCommon::HEADER_SIZE);
_crc32 = getCRCFromHeader();

View File

@@ -103,18 +103,22 @@ void ZipLocalFileHeader::parse(std::istream& inp, bool assumeHeaderRead)
if (!assumeHeaderRead)
{
inp.read(_rawHeader, ZipCommon::HEADER_SIZE);
if (inp.gcount() != ZipCommon::HEADER_SIZE)
throw Poco::IOException("Failed to read local file header");
if (std::memcmp(_rawHeader, HEADER, ZipCommon::HEADER_SIZE) != 0)
throw Poco::DataFormatException("Bad local file header");
}
else
{
std::memcpy(_rawHeader, HEADER, ZipCommon::HEADER_SIZE);
}
poco_assert (std::memcmp(_rawHeader, HEADER, ZipCommon::HEADER_SIZE) == 0);
// read the rest of the header
inp.read(_rawHeader + ZipCommon::HEADER_SIZE, FULLHEADER_SIZE - ZipCommon::HEADER_SIZE);
if (!(_rawHeader[VERSION_POS + 1]>= ZipCommon::HS_FAT && _rawHeader[VERSION_POS + 1] < ZipCommon::HS_UNUSED))
throw Poco::DataFormatException("bad ZIP file header", "invalid version");
throw Poco::DataFormatException("Bad local file header", "invalid version");
if (ZipUtil::get16BitValue(_rawHeader, COMPR_METHOD_POS) >= ZipCommon::CM_UNUSED)
throw Poco::DataFormatException("bad ZIP file header", "invalid compression method");
throw Poco::DataFormatException("Bad local file header", "invalid compression method");
parseDateTime();
Poco::UInt16 len = getFileNameLength();
if (len > 0)

View File

@@ -111,7 +111,7 @@ const ZipLocalFileHeader& ZipManipulator::getForChange(const std::string& zipPat
{
ZipArchive::FileHeaders::const_iterator it = _in->findHeader(zipPath);
if (it == _in->headerEnd())
throw ZipManipulationException("entry not found: " + zipPath);
throw ZipManipulationException("Entry not found: " + zipPath);
if (_changes.find(zipPath) != _changes.end())
throw ZipManipulationException("A change request exists already for entry " + zipPath);