fix redundant assert in gzip header check

This commit is contained in:
Arvid Norberg 2017-09-29 15:59:57 -07:00 committed by Arvid Norberg
parent 86c37af147
commit 61733787f7
2 changed files with 12 additions and 5 deletions

View File

@ -114,16 +114,14 @@ namespace {
// returns -1 if gzip header is invalid or the header size in bytes // returns -1 if gzip header is invalid or the header size in bytes
int gzip_header(span<char const> const buf) int gzip_header(span<char const> const buf)
{ {
TORRENT_ASSERT(!buf.empty()); // The zip header cannot be shorter than 10 bytes
if (buf.size() < 10) return -1;
span<unsigned char const> buffer( span<unsigned char const> buffer(
reinterpret_cast<const unsigned char*>(buf.data()), buf.size()); reinterpret_cast<const unsigned char*>(buf.data()), buf.size());
// gzip is defined in https://tools.ietf.org/html/rfc1952 // gzip is defined in https://tools.ietf.org/html/rfc1952
// The zip header cannot be shorter than 10 bytes
if (buffer.size() < 10) return -1;
// check the magic header of gzip // check the magic header of gzip
if ((buffer[0] != GZIP_MAGIC0) || (buffer[1] != GZIP_MAGIC1)) return -1; if ((buffer[0] != GZIP_MAGIC0) || (buffer[1] != GZIP_MAGIC1)) return -1;
@ -191,7 +189,7 @@ namespace {
ec.clear(); ec.clear();
TORRENT_ASSERT(maximum_size > 0); TORRENT_ASSERT(maximum_size > 0);
int header_len = gzip_header(in); int const header_len = gzip_header(in);
if (header_len < 0) if (header_len < 0)
{ {
ec = gzip_errors::invalid_gzip_header; ec = gzip_errors::invalid_gzip_header;

View File

@ -74,3 +74,12 @@ TORRENT_TEST(corrupt)
TEST_CHECK(ec); TEST_CHECK(ec);
} }
TORRENT_TEST(empty)
{
std::vector<char> empty;
std::vector<char> inflated;
error_code ec;
inflate_gzip(empty, inflated, 1000000, ec);
TEST_CHECK(ec);
}