diff --git a/src/gzip.cpp b/src/gzip.cpp index e4515b3dd..3326f3c03 100644 --- a/src/gzip.cpp +++ b/src/gzip.cpp @@ -114,16 +114,14 @@ namespace { // returns -1 if gzip header is invalid or the header size in bytes int gzip_header(span const buf) { - TORRENT_ASSERT(!buf.empty()); + // The zip header cannot be shorter than 10 bytes + if (buf.size() < 10) return -1; span buffer( reinterpret_cast(buf.data()), buf.size()); // 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 if ((buffer[0] != GZIP_MAGIC0) || (buffer[1] != GZIP_MAGIC1)) return -1; @@ -191,7 +189,7 @@ namespace { ec.clear(); TORRENT_ASSERT(maximum_size > 0); - int header_len = gzip_header(in); + int const header_len = gzip_header(in); if (header_len < 0) { ec = gzip_errors::invalid_gzip_header; diff --git a/test/test_gzip.cpp b/test/test_gzip.cpp index 72aad861c..7ed85ab95 100644 --- a/test/test_gzip.cpp +++ b/test/test_gzip.cpp @@ -74,3 +74,12 @@ TORRENT_TEST(corrupt) TEST_CHECK(ec); } +TORRENT_TEST(empty) +{ + std::vector empty; + std::vector inflated; + error_code ec; + inflate_gzip(empty, inflated, 1000000, ec); + TEST_CHECK(ec); +} +